SmallJoker
2022-10-25 c40189eabf663eb142e5da8107a570c4a0018642
commit | author | age
d62371 1
BW 2 -- LV Lamp
3 -- Illuminates a 7x7x3(H) volume below itself with light bright as the sun.
4
5
6 local S = technic.getter
7
8 local desc = S("@1 Lamp", S("LV"))
9 local active_desc = S("@1 Active", desc)
10 local unpowered_desc = S("@1 Unpowered", desc)
11 local off_desc = S("@1 Off", desc)
12 local demand = 50
13
14
15 -- Invisible light source node used for illumination
16 minetest.register_node("technic:dummy_light_source", {
17     description = S("Dummy light source node"),
18     inventory_image = "technic_dummy_light_source.png",
19     wield_image = "technic_dummy_light_source.png",
20     paramtype = "light",
21     drawtype = "airlike",
22     light_source = 14,
23     sunlight_propagates = true,
24     walkable = false,
25     buildable_to = true,
26     diggable = false,
27     pointable = false,
28     --drop = "",  -- Intentionally allowed to drop itself
29     groups = {not_in_creative_inventory = 1}
30 })
31
32
33 local function illuminate(pos, active)
34     local pos1 = {x = pos.x - 3, y = pos.y - 1, z = pos.z - 3}
35     local pos2 = {x = pos.x + 3, y = pos.y - 3, z = pos.z + 3}
36
37     local find_node = active and "air" or "technic:dummy_light_source"
38     local set_node = {name = (active and "technic:dummy_light_source" or "air")}
39
40     for _,p in pairs(minetest.find_nodes_in_area(pos1, pos2, find_node)) do
41         minetest.set_node(p, set_node)
42     end
43 end
44
45 local function lamp_run(pos, node)
46     local meta = minetest.get_meta(pos)
47
48     if meta:get_int("LV_EU_demand") == 0 then
49         return  -- Lamp is turned off
50     end
51
52     local eu_input = meta:get_int("LV_EU_input")
53
54     if node.name == "technic:lv_lamp_active" then
55         if eu_input < demand then
56             technic.swap_node(pos, "technic:lv_lamp")
57             meta:set_string("infotext", unpowered_desc)
58             illuminate(pos, false)
59         else
60             illuminate(pos, true)
61         end
62     elseif node.name == "technic:lv_lamp" then
63         if eu_input >= demand then
64             technic.swap_node(pos, "technic:lv_lamp_active")
65             meta:set_string("infotext", active_desc)
66             illuminate(pos, true)
67         end
68     end
69 end
70
71 local function lamp_toggle(pos, node, player)
72     if not player or minetest.is_protected(pos, player:get_player_name()) then
73         return
74     end
75     local meta = minetest.get_meta(pos)
76     if meta:get_int("LV_EU_demand") == 0 then
77         meta:set_string("infotext", active_desc)
78         meta:set_int("LV_EU_demand", demand)
79     else
80         illuminate(pos, false)
81         technic.swap_node(pos, "technic:lv_lamp")
82         meta:set_string("infotext", off_desc)
83         meta:set_int("LV_EU_demand", 0)
84     end
85 end
86
4775d9 87 local common_fields = {
d62371 88     drawtype = "nodebox",
BW 89     node_box = {
90         type = "fixed",
91         fixed = {0.5,0.5,0.5,-0.5,-0.2,-0.5}
92     },
93     tiles = {
94         "technic_lv_lamp_top.png",
95         "technic_lv_lamp_bottom.png",
96         "technic_lv_lamp_side.png",
97         "technic_lv_lamp_side.png",
98         "technic_lv_lamp_side.png",
99         "technic_lv_lamp_side.png"
100     },
101     connect_sides = {"front", "back", "left", "right", "top"},
102     can_dig = technic.machine_can_dig,
103     technic_run = lamp_run,
4775d9 104     on_destruct = illuminate,
S 105     on_rightclick = lamp_toggle
106 }
107
108 local ndef
109
110 ndef = {
111     description = desc,
112     groups = {cracky = 2, technic_machine = 1, technic_lv = 1},
d62371 113     on_construct = function(pos)
BW 114         local meta = minetest.get_meta(pos)
115         meta:set_string("infotext", desc)
116         meta:set_int("LV_EU_demand", demand)
4775d9 117     end
S 118 }
d62371 119
4775d9 120 for k, v in pairs(common_fields) do
S 121     ndef[k] = v
122 end
123
124 minetest.register_node("technic:lv_lamp", ndef)
125
126
127 ndef = {
d62371 128     description = active_desc,
BW 129     paramtype = "light",
130     light_source = 14,
131     drop = "technic:lv_lamp",
132     groups = {cracky = 2, technic_machine = 1, technic_lv = 1, not_in_creative_inventory = 1},
133     technic_on_disable = function(pos)
134         illuminate(pos, false)
135         technic.swap_node(pos, "technic:lv_lamp")
136     end,
4775d9 137 }
S 138
139 for k, v in pairs(common_fields) do
140     ndef[k] = v
141 end
142
143 minetest.register_node("technic:lv_lamp_active", ndef)
144
d62371 145
BW 146 technic.register_machine("LV", "technic:lv_lamp", technic.receiver)
147 technic.register_machine("LV", "technic:lv_lamp_active", technic.receiver)
148
149 minetest.register_craft({
150     output = "technic:lv_lamp",
151     recipe = {
152         {"default:glass", "default:glass", "default:glass"},
153         {"technic:lv_led", "technic:lv_led", "technic:lv_led"},
154         {"mesecons_materials:glue", "technic:lv_cable", "mesecons_materials:glue"},
155     }
156 })