techage/lamps/lib.lua

126 lines
3.5 KiB
Lua
Raw Normal View History

2019-05-21 23:04:05 +03:00
-- for lazy programmers
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local P = minetest.string_to_pos
local M = minetest.get_meta
2019-06-16 22:06:16 +03:00
local PWR_NEEDED = 0.5
2019-06-20 15:01:37 +03:00
local CYCLE_TIME = 4
2019-05-21 23:04:05 +03:00
2019-06-16 22:06:16 +03:00
local Cable = techage.ElectricCable
local consume_power = techage.power.consume_power
2019-06-17 20:57:08 +03:00
local power_available = techage.power.power_available
2019-05-21 23:04:05 +03:00
local function swap_node(pos, postfix)
2019-06-09 01:25:25 +03:00
local node = techage.get_node_lvm(pos)
2019-05-21 23:04:05 +03:00
local parts = string.split(node.name, "_")
if postfix == parts[2] then
return
end
node.name = parts[1].."_"..postfix
minetest.swap_node(pos, node)
end
2019-06-20 15:01:37 +03:00
local function on_power(pos)
2019-06-16 22:06:16 +03:00
local mem = tubelib2.get_mem(pos)
2019-06-20 15:01:37 +03:00
if mem.turned_on then
2019-06-16 22:06:16 +03:00
local got = consume_power(pos, PWR_NEEDED)
2019-06-20 15:01:37 +03:00
if got < PWR_NEEDED and mem.node_on then
2019-06-16 22:06:16 +03:00
swap_node(pos, "off")
2019-06-20 15:01:37 +03:00
mem.node_on = false
elseif not mem.node_on then
2019-06-16 22:06:16 +03:00
swap_node(pos, "on")
2019-06-20 15:01:37 +03:00
mem.node_on = true
2019-06-16 22:06:16 +03:00
end
2019-06-20 15:01:37 +03:00
mem.trigger = true
2019-05-21 23:04:05 +03:00
end
2019-06-20 15:01:37 +03:00
end
local function node_timer(pos, elapsed)
local mem = tubelib2.get_mem(pos)
if mem.node_on and not mem.trigger then
mem.node_on = false
swap_node(pos, "off")
end
mem.trigger = false
return mem.turned_on
2019-05-21 23:04:05 +03:00
end
local function lamp_on_rightclick(pos, node, clicker)
if minetest.is_protected(pos, clicker:get_player_name()) then
return
end
local mem = tubelib2.get_mem(pos)
2019-06-20 15:01:37 +03:00
mem.turned_on = not mem.turned_on
if mem.turned_on and power_available(pos, PWR_NEEDED) then
mem.node_on = true
2019-06-17 20:57:08 +03:00
swap_node(pos, "on")
2019-06-20 15:01:37 +03:00
mem.trigger = true
minetest.get_node_timer(pos):start(CYCLE_TIME)
elseif mem.turned_on then
mem.node_on = false
swap_node(pos, "off")
2019-06-16 22:06:16 +03:00
minetest.get_node_timer(pos):start(CYCLE_TIME)
2019-05-21 23:04:05 +03:00
else
2019-06-20 15:01:37 +03:00
mem.node_on = false
2019-06-16 22:06:16 +03:00
swap_node(pos, "off")
2019-06-20 15:01:37 +03:00
minetest.get_node_timer(pos):stop()
2019-05-21 23:04:05 +03:00
end
end
2019-05-27 00:22:29 +03:00
local function on_rotate(pos, node, user, mode, new_param2)
if minetest.is_protected(pos, user:get_player_name()) then
return false
end
2019-06-15 17:14:46 +03:00
node.param2 = techage.rotate_wallmounted(node.param2)
2019-05-27 00:22:29 +03:00
minetest.swap_node(pos, node)
return true
end
local function on_place(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then
return itemstack
end
return minetest.rotate_and_place(itemstack, placer, pointed_thing)
end
local function determine_power_side(pos, node)
2019-06-15 17:14:46 +03:00
return {techage.determine_node_bottom_as_dir(node)}
2019-05-27 00:22:29 +03:00
end
2019-05-21 23:04:05 +03:00
function techage.register_lamp(basename, ndef_off, ndef_on)
ndef_off.on_construct = tubelib2.init_mem
ndef_off.on_rightclick = lamp_on_rightclick
2019-05-27 00:22:29 +03:00
ndef_off.on_rotate = on_rotate
2019-06-16 22:06:16 +03:00
ndef_off.on_timer = node_timer
2019-05-27 00:22:29 +03:00
ndef_off.on_place = on_place
ndef_off.paramtype = "light"
ndef_off.light_source = 0
ndef_off.sunlight_propagates = true
ndef_off.paramtype2 = "facedir"
ndef_off.groups = {choppy=2, cracky=2, crumbly=2}
ndef_off.is_ground_content = false
ndef_off.sounds = default.node_sound_glass_defaults()
2019-05-21 23:04:05 +03:00
ndef_on.on_construct = tubelib2.init_mem
ndef_on.on_rightclick = lamp_on_rightclick
2019-05-27 00:22:29 +03:00
ndef_on.on_rotate = on_rotate
2019-06-16 22:06:16 +03:00
ndef_on.on_timer = node_timer
2019-05-27 00:22:29 +03:00
ndef_on.paramtype = "light"
ndef_on.light_source = minetest.LIGHT_MAX
ndef_on.sunlight_propagates = true
ndef_on.paramtype2 = "facedir"
ndef_on.diggable = false
ndef_on.groups = {not_in_creative_inventory=1}
ndef_on.is_ground_content = false
ndef_on.sounds = default.node_sound_glass_defaults()
2019-05-21 23:04:05 +03:00
minetest.register_node(basename.."_off", ndef_off)
minetest.register_node(basename.."_on", ndef_on)
techage.power.register_node({basename.."_off", basename.."_on"}, {
2019-06-16 22:06:16 +03:00
power_network = Cable,
2019-06-10 22:31:58 +03:00
conn_sides = ndef_off.conn_sides or determine_power_side, -- will be handled by clbk function
2019-06-20 15:01:37 +03:00
on_power = on_power,
2019-05-21 23:04:05 +03:00
})
end