techage/.test/source.lua

63 lines
1.8 KiB
Lua
Raw Normal View History

2019-06-15 17:14:46 +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
local CYCLE_TIME = 2
local PWR_CAPA = 15
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 node_timer(pos, elapsed)
--print("node_timer source "..S(pos))
local mem = tubelib2.get_mem(pos)
if mem.generating then
2019-08-15 18:06:58 +03:00
local provided = power.generator_alive(pos, mem)
--print("provided", provided)
2019-06-15 17:14:46 +03:00
end
2019-08-15 18:06:58 +03:00
return mem.generating
2019-06-15 17:14:46 +03:00
end
local function on_rightclick(pos, node, clicker)
local mem = tubelib2.get_mem(pos)
if not mem.generating then
mem.generating = true
2019-08-15 18:06:58 +03:00
power.generator_start(pos, mem, PWR_CAPA)
2019-06-15 17:14:46 +03:00
minetest.get_node_timer(pos):start(CYCLE_TIME)
2019-08-15 18:06:58 +03:00
M(pos):set_string("infotext", "on")
2019-06-15 17:14:46 +03:00
else
mem.generating = false
2019-08-15 18:06:58 +03:00
power.generator_stop(pos, mem)
minetest.get_node_timer(pos):stop()
M(pos):set_string("infotext", "off")
2019-06-15 17:14:46 +03:00
end
end
minetest.register_node("techage:source", {
description = "Source",
tiles = {
-- up, down, right, left, back, front
'techage_electric_button.png^techage_appl_electronic_fab.png',
'techage_electric_button.png^techage_appl_electronic_fab.png',
'techage_electric_button.png^techage_appl_electronic_fab.png^techage_electric_plug.png',
'techage_electric_button.png^techage_appl_electronic_fab.png',
'techage_electric_button.png^techage_appl_electronic_fab.png',
'techage_electric_button.png^techage_appl_electronic_fab.png',
},
2019-08-15 18:06:58 +03:00
2019-06-15 17:14:46 +03:00
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
is_ground_content = false,
on_rightclick = on_rightclick,
on_timer = node_timer,
})
2019-08-08 15:43:45 +03:00
techage.power.register_node({"techage:source"}, {
2019-06-15 17:14:46 +03:00
power_network = Cable,
after_place_node = function(pos)
local mem = tubelib2.init_mem(pos)
M(pos):set_string("infotext", "off")
end,
2019-06-15 17:14:46 +03:00
})