techage/.test/sink.lua

113 lines
2.6 KiB
Lua
Raw Normal View History

2020-01-26 01:15:44 +03:00
--[[
TechAge
=======
2021-06-09 23:09:59 +03:00
Copyright (C) 2019-2021 Joachim Stolberg
2020-01-26 01:15:44 +03:00
GPL v3
See LICENSE.txt for more information
2022-01-03 23:40:31 +03:00
2020-01-26 01:15:44 +03:00
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
2021-06-05 12:42:30 +03:00
--local Cable = techage.Axle
local power = networks.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
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
2021-06-05 12:42:30 +03:00
swap_node(pos, "techage:sink_on")
M(pos):set_string("infotext", "on")
2020-01-26 01:15:44 +03:00
minetest.get_node_timer(pos):start(CYCLE_TIME)
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")
2019-08-15 18:06:58 +03:00
M(pos):set_string("infotext", "off")
2021-06-05 12:42:30 +03:00
minetest.get_node_timer(pos):stop()
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
2019-06-15 17:14:46 +03:00
minetest.register_node("techage:sink", {
description = "Sink",
2021-06-05 21:18:04 +03:00
tiles = {'techage_electric_button.png^[colorize:#000000:50'},
2022-01-03 23:40:31 +03:00
2021-06-05 12:42:30 +03:00
on_timer = function(pos, elapsed)
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)
if consumed == PWR_NEEDED then
swap_node(pos, "techage:sink_on")
M(pos):set_string("infotext", "on")
end
return true
end,
2019-06-15 17:14:46 +03:00
on_rightclick = on_rightclick,
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2019-06-15 17:14:46 +03:00
paramtype = "light",
2022-01-03 23:40:31 +03:00
light_source = 0,
2019-06-15 17:14:46 +03:00
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'},
2021-06-05 12:42:30 +03:00
on_timer = function(pos, elapsed)
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)
if consumed < PWR_NEEDED then
swap_node(pos, "techage:sink")
M(pos):set_string("infotext", "off")
end
return true
end,
2019-06-15 17:14:46 +03:00
on_rightclick = on_rightclick,
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2019-06-15 17:14:46 +03:00
paramtype = "light",
2022-01-03 23:40:31 +03:00
light_source = minetest.LIGHT_MAX,
2019-06-15 17:14:46 +03:00
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(),
})
power.register_nodes({"techage:sink", "techage:sink_on"}, Cable, "con")