2019-08-22 21:49:47 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
TechAge
|
|
|
|
=======
|
|
|
|
|
|
|
|
Copyright (C) 2019 Joachim Stolberg
|
|
|
|
|
2020-10-19 20:09:17 +03:00
|
|
|
AGPL v3
|
2019-08-22 21:49:47 +03:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
Lamp library
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
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
|
2020-01-31 21:55:10 +03:00
|
|
|
local CYCLE_TIME = 2
|
2019-05-21 23:04:05 +03:00
|
|
|
|
2019-06-16 22:06:16 +03:00
|
|
|
local Cable = techage.ElectricCable
|
2021-06-07 23:00:52 +03:00
|
|
|
local power = networks.power
|
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)
|
2020-03-27 21:44:19 +03:00
|
|
|
local ndef = minetest.registered_nodes[node.name]
|
|
|
|
if ndef.on_switch_lamp then
|
|
|
|
ndef.on_switch_lamp(pos, postfix == "on")
|
|
|
|
end
|
2019-05-21 23:04:05 +03:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
local function on_power(pos)
|
|
|
|
swap_node(pos, "on")
|
2020-05-01 15:43:08 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
nvm.turned_on = true
|
2019-06-20 15:01:37 +03:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
local function on_nopower(pos)
|
2019-08-17 00:44:11 +03:00
|
|
|
swap_node(pos, "off")
|
2020-05-01 15:43:08 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
nvm.turned_on = false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_running(pos, nvm)
|
|
|
|
return nvm.turned_on
|
2019-08-17 00:44:11 +03:00
|
|
|
end
|
|
|
|
|
2021-06-07 23:00:52 +03:00
|
|
|
local function node_timer_off1(pos, elapsed)
|
|
|
|
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)
|
|
|
|
if consumed == PWR_NEEDED then
|
|
|
|
on_power(pos)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function node_timer_off2(pos, elapsed)
|
|
|
|
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED * 2)
|
|
|
|
if consumed == PWR_NEEDED * 2 then
|
|
|
|
on_power(pos)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function node_timer_on1(pos, elapsed)
|
|
|
|
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)
|
|
|
|
if consumed < PWR_NEEDED then
|
|
|
|
on_nopower(pos)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function node_timer_on2(pos, elapsed)
|
|
|
|
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED * 2)
|
|
|
|
if consumed < PWR_NEEDED * 2 then
|
|
|
|
on_nopower(pos)
|
|
|
|
end
|
2020-01-31 21:55:10 +03:00
|
|
|
return true
|
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
|
2019-08-17 00:44:11 +03:00
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
if not nvm.turned_on and power.power_available(pos, Cable) then
|
|
|
|
nvm.turned_on = true
|
|
|
|
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
2019-10-05 20:19:18 +03:00
|
|
|
swap_node(pos, "on")
|
2019-05-21 23:04:05 +03:00
|
|
|
else
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.turned_on = false
|
|
|
|
minetest.get_node_timer(pos):stop()
|
2019-10-05 20:19:18 +03:00
|
|
|
swap_node(pos, "off")
|
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
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
local function after_place_node(pos)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
Cable:after_place_node(pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function after_dig_node(pos, oldnode)
|
|
|
|
Cable:after_dig_node(pos)
|
|
|
|
techage.del_mem(pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function tubelib2_on_update2(pos, outdir, tlib2, node)
|
|
|
|
power.update_network(pos, outdir, tlib2)
|
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)
|
2020-03-28 15:22:12 +03:00
|
|
|
if ndef_off.high_power then
|
2021-06-07 23:00:52 +03:00
|
|
|
ndef_off.on_timer = ndef_off.on_timer or node_timer_off2
|
2020-03-28 15:22:12 +03:00
|
|
|
else
|
2021-06-07 23:00:52 +03:00
|
|
|
ndef_off.on_timer = ndef_off.on_timer or node_timer_off1
|
2020-03-28 15:22:12 +03:00
|
|
|
end
|
2021-06-07 23:00:52 +03:00
|
|
|
ndef_off.after_place_node = after_place_node
|
|
|
|
ndef_off.after_dig_node = after_dig_node
|
2019-05-21 23:04:05 +03:00
|
|
|
ndef_off.on_rightclick = lamp_on_rightclick
|
2019-06-25 21:35:51 +03:00
|
|
|
if not ndef_off.on_rotate then
|
|
|
|
ndef_off.on_place = on_place
|
|
|
|
end
|
|
|
|
ndef_off.on_rotate = ndef_off.on_rotate or on_rotate
|
2019-05-27 00:22:29 +03:00
|
|
|
ndef_off.paramtype = "light"
|
2021-03-19 22:06:22 +03:00
|
|
|
ndef_off.use_texture_alpha = techage.CLIP
|
2019-05-27 00:22:29 +03:00
|
|
|
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
|
|
|
|
2020-03-28 15:22:12 +03:00
|
|
|
if ndef_on.high_power then
|
2021-06-07 23:00:52 +03:00
|
|
|
ndef_on.on_timer = ndef_on.on_timer or node_timer_on2
|
2020-03-28 15:22:12 +03:00
|
|
|
else
|
2021-06-07 23:00:52 +03:00
|
|
|
ndef_on.on_timer = ndef_on.on_timer or node_timer_on1
|
2020-03-28 15:22:12 +03:00
|
|
|
end
|
2021-06-07 23:00:52 +03:00
|
|
|
ndef_on.after_place_node = after_place_node
|
|
|
|
ndef_on.after_dig_node = after_dig_node
|
2019-05-21 23:04:05 +03:00
|
|
|
ndef_on.on_rightclick = lamp_on_rightclick
|
2019-06-25 21:35:51 +03:00
|
|
|
ndef_on.on_rotate = ndef_on.on_rotate or on_rotate
|
2019-05-27 00:22:29 +03:00
|
|
|
ndef_on.paramtype = "light"
|
2021-03-19 22:06:22 +03:00
|
|
|
ndef_on.use_texture_alpha = techage.CLIP
|
2019-05-27 00:22:29 +03:00
|
|
|
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)
|
2020-01-31 21:55:10 +03:00
|
|
|
|
2021-06-07 23:00:52 +03:00
|
|
|
power.register_nodes({basename.."_off", basename.."_on"}, Cable, "con")
|
2019-05-21 23:04:05 +03:00
|
|
|
end
|
2019-09-08 22:54:34 +03:00
|
|
|
|