techage/.test/sink.lua

106 lines
2.6 KiB
Lua
Raw Normal View History

2019-06-15 07:14:46 -07: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-08-08 05:43:45 -07:00
local PWR_NEEDED = 5
local CYCLE_TIME = 4
2019-06-15 07:14:46 -07:00
local Cable = techage.ElectricCable
2019-08-15 08:06:58 -07:00
local power = techage.power
2019-06-15 07:14:46 -07:00
local function swap_node(pos, name)
local node = minetest.get_node(pos)
if node.name == name then
return
end
node.name = name
minetest.swap_node(pos, node)
end
2019-08-15 08:06:58 -07:00
local function on_power(pos, mem)
2019-08-17 04:33:46 -07:00
mydbg("dbg", "on_power")
2019-08-15 08:06:58 -07:00
if mem.running then
swap_node(pos, "techage:sink_on")
minetest.get_node_timer(pos):start(CYCLE_TIME)
M(pos):set_string("infotext", "on")
2019-06-15 07:14:46 -07:00
end
2019-08-08 05:43:45 -07:00
end
2019-08-15 08:06:58 -07:00
local function on_nopower(pos, mem)
2019-08-17 04:33:46 -07:00
mydbg("dbg", "on_nopower")
2019-08-15 08:06:58 -07:00
swap_node(pos, "techage:sink")
M(pos):set_string("infotext", "nopower")
end
2019-08-08 05:43:45 -07:00
local function node_timer(pos, elapsed)
--print("node_timer sink "..S(pos))
local mem = tubelib2.get_mem(pos)
2019-08-15 08:06:58 -07:00
power.consumer_alive(pos, mem)
return mem.running
2019-06-15 07:14:46 -07:00
end
local function on_rightclick(pos, node, clicker)
local mem = tubelib2.get_mem(pos)
2019-08-15 08:06:58 -07:00
if not mem.running and power.power_available(pos, mem, PWR_NEEDED) then
2019-06-15 07:14:46 -07:00
mem.running = true
2019-08-17 04:33:46 -07:00
mydbg("dbg", "turn on")
2019-08-16 02:13:30 -07:00
--swap_node(pos, "techage:sink_on")
2019-08-15 08:06:58 -07:00
power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED)
2019-06-15 07:14:46 -07:00
minetest.get_node_timer(pos):start(CYCLE_TIME)
2019-08-15 08:06:58 -07:00
M(pos):set_string("infotext", "on")
2019-06-15 07:14:46 -07:00
else
2019-08-15 08:06:58 -07:00
mem.running = false
2019-08-17 04:33:46 -07:00
mydbg("dbg", "turn off")
2019-06-15 07:14:46 -07:00
swap_node(pos, "techage:sink")
2019-08-15 08:06:58 -07:00
power.consumer_stop(pos, mem)
2019-06-15 07:14:46 -07:00
minetest.get_node_timer(pos):stop()
2019-08-15 08:06:58 -07:00
M(pos):set_string("infotext", "off")
2019-06-15 07:14:46 -07:00
end
end
minetest.register_node("techage:sink", {
description = "Sink",
tiles = {'techage_electric_button.png'},
2019-08-15 08:06:58 -07:00
after_place_node = function(pos)
local mem = tubelib2.init_mem(pos)
2019-08-15 08:06:58 -07:00
M(pos):set_string("infotext", "off")
end,
2019-06-15 07:14:46 -07:00
on_timer = node_timer,
on_rightclick = on_rightclick,
paramtype = "light",
light_source = 0,
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:sink_on", {
description = "Sink",
tiles = {'techage_electric_button.png'},
on_timer = node_timer,
on_rightclick = on_rightclick,
paramtype = "light",
light_source = minetest.LIGHT_MAX,
sunlight_propagates = true,
paramtype2 = "facedir",
diggable = false,
drop = "",
groups = {not_in_creative_inventory=1},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
2019-08-08 05:43:45 -07:00
techage.power.register_node({"techage:sink", "techage:sink_on"}, {
2019-06-15 07:14:46 -07:00
power_network = Cable,
2019-08-08 05:43:45 -07:00
on_power = on_power,
2019-08-15 08:06:58 -07:00
on_nopower = on_nopower,
2019-06-15 07:14:46 -07:00
})