techage/.test/sink.lua

138 lines
3.1 KiB
Lua
Raw Normal View History

2020-01-26 01:15:44 +03:00
--[[
TechAge
=======
Copyright (C) 2019-2020 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
Demo for a electrical power consuming node
]]--
2019-06-15 17:14:46 +03:00
-- for lazy programmers
2020-01-26 01:15:44 +03:00
local P2S = minetest.pos_to_string
2019-06-15 17:14:46 +03:00
local M = minetest.get_meta
2020-01-26 01:15:44 +03:00
local S = techage.S
2019-06-15 17:14:46 +03:00
2019-08-08 15:43:45 +03:00
local PWR_NEEDED = 5
2020-01-26 01:15:44 +03:00
local CYCLE_TIME = 2
2019-06-15 17:14:46 +03:00
local Cable = techage.ElectricCable
2019-08-15 18:06:58 +03:00
local power = techage.power
2019-06-15 17:14:46 +03:00
local function swap_node(pos, name)
local node = techage.get_node_lvm(pos)
2019-06-15 17:14:46 +03:00
if node.name == name then
return
end
node.name = name
minetest.swap_node(pos, node)
end
2020-01-26 01:15:44 +03:00
local function on_power(pos)
2020-01-26 22:15:40 +03:00
--print("on_power sink "..P2S(pos))
2020-01-26 01:15:44 +03:00
swap_node(pos, "techage:sink_on")
M(pos):set_string("infotext", "on")
2019-08-08 15:43:45 +03:00
end
2020-01-26 01:15:44 +03:00
local function on_nopower(pos)
2020-01-26 22:15:40 +03:00
--print("on_nopower sink "..P2S(pos))
2019-08-15 18:06:58 +03:00
swap_node(pos, "techage:sink")
2020-01-26 01:15:44 +03:00
M(pos):set_string("infotext", "off")
2019-08-15 18:06:58 +03:00
end
2019-08-08 15:43:45 +03:00
local function node_timer(pos, elapsed)
2020-01-26 01:15:44 +03:00
--print("node_timer sink "..P2S(pos))
local nvm = techage.get_nvm(pos)
power.consumer_alive(pos, Cable, CYCLE_TIME)
return true
2019-06-15 17:14:46 +03:00
end
local function on_rightclick(pos, node, clicker)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
if not nvm.running and power.power_available(pos, Cable) then
nvm.running = true
-- swap will be performed via on_power()
power.consumer_start(pos, Cable, CYCLE_TIME)
minetest.get_node_timer(pos):start(CYCLE_TIME)
2019-11-11 23:25:20 +03:00
M(pos):set_string("infotext", "...")
2019-06-15 17:14:46 +03:00
else
2020-01-26 01:15:44 +03:00
nvm.running = false
2019-06-15 17:14:46 +03:00
swap_node(pos, "techage:sink")
2020-01-26 01:15:44 +03:00
power.consumer_stop(pos, Cable)
2019-06-15 17:14:46 +03:00
minetest.get_node_timer(pos):stop()
2019-08-15 18:06:58 +03:00
M(pos):set_string("infotext", "off")
2019-06-15 17:14:46 +03:00
end
end
local function after_place_node(pos)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
M(pos):set_string("infotext", "off")
2020-01-26 01:15:44 +03:00
Cable:after_place_node(pos)
end
local function after_dig_node(pos, oldnode)
2020-01-26 01:15:44 +03:00
Cable:after_dig_node(pos)
techage.del_mem(pos)
end
2020-01-26 01:15:44 +03:00
local function tubelib2_on_update2(pos, outdir, tlib2, node)
power.update_network(pos, outdir, tlib2)
end
2019-06-15 17:14:46 +03:00
2020-01-26 01:15:44 +03:00
local net_def = {
ele1 = {
sides = techage.networks.AllSides, -- Cable connection sides
ntype = "con1",
on_power = on_power,
on_nopower = on_nopower,
nominal = PWR_NEEDED,
},
}
2019-06-15 17:14:46 +03:00
minetest.register_node("techage:sink", {
description = "Sink",
tiles = {'techage_electric_button.png'},
2019-08-15 18:06:58 +03:00
2019-06-15 17:14:46 +03:00
on_timer = node_timer,
on_rightclick = on_rightclick,
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2020-01-26 01:15:44 +03:00
tubelib2_on_update2 = tubelib2_on_update2,
networks = net_def,
2019-06-15 17:14:46 +03:00
paramtype = "light",
light_source = 0,
paramtype2 = "facedir",
2020-01-26 01:15:44 +03:00
groups = {choppy = 2, cracky = 2, crumbly = 2},
2019-06-15 17:14:46 +03:00
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:sink_on", {
description = "Sink",
tiles = {'techage_electric_button.png'},
2019-06-15 17:14:46 +03:00
on_timer = node_timer,
on_rightclick = on_rightclick,
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2020-01-26 01:15:44 +03:00
tubelib2_on_update2 = tubelib2_on_update2,
networks = net_def,
2019-06-15 17:14:46 +03:00
paramtype = "light",
light_source = minetest.LIGHT_MAX,
paramtype2 = "facedir",
diggable = false,
drop = "",
2020-01-26 01:15:44 +03:00
groups = {not_in_creative_inventory = 1},
2019-06-15 17:14:46 +03:00
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
2020-01-26 01:15:44 +03:00
Cable:add_secondary_node_names({"techage:sink", "techage:sink_on"})