Bryce w
2022-10-20 d623715d94152e38e817555f43c7c6ab2372045c
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
87 minetest.register_node("technic:lv_lamp", {
88     description = desc,
89     drawtype = "nodebox",
90     node_box = {
91         type = "fixed",
92         fixed = {0.5,0.5,0.5,-0.5,-0.2,-0.5}
93     },
94     collision_box = {
95         type = "fixed",
96         fixed = {0.5,0.5,0.5,-0.5,-0.2,-0.5}
97     },
98     selection_box = {
99         type = "fixed",
100         fixed = {0.5,0.5,0.5,-0.5,-0.2,-0.5}
101     },
102     tiles = {
103         "technic_lv_lamp_top.png",
104         "technic_lv_lamp_bottom.png",
105         "technic_lv_lamp_side.png",
106         "technic_lv_lamp_side.png",
107         "technic_lv_lamp_side.png",
108         "technic_lv_lamp_side.png"
109     },
110     groups = {cracky = 2, technic_machine = 1, technic_lv = 1},
111     connect_sides = {"front", "back", "left", "right", "top"},
112     can_dig = technic.machine_can_dig,
113     technic_run = lamp_run,
114     on_construct = function(pos)
115         local meta = minetest.get_meta(pos)
116         meta:set_string("infotext", desc)
117         meta:set_int("LV_EU_demand", demand)
118     end,
119     on_destruct = illuminate,
120     on_rightclick = lamp_toggle
121 })
122
123 minetest.register_node("technic:lv_lamp_active", {
124     description = active_desc,
125     drawtype = "nodebox",
126     node_box = {
127         type = "fixed",
128         fixed = {0.5, 0.5, 0.5, -0.5, -0.2, -0.5}
129     },
130     collision_box = {
131         type = "fixed",
132         fixed = {0.5, 0.5, 0.5, -0.5, -0.2, -0.5}
133     },
134     selection_box = {
135         type = "fixed",
136         fixed = {0.5, 0.5, 0.5, -0.5, -0.2, -0.5}
137     },
138     tiles = {
139         "technic_lv_lamp_top.png",
140         "technic_lv_lamp_bottom.png",
141         "technic_lv_lamp_side.png",
142         "technic_lv_lamp_side.png",
143         "technic_lv_lamp_side.png",
144         "technic_lv_lamp_side.png"
145     },
146     paramtype = "light",
147     light_source = 14,
148     drop = "technic:lv_lamp",
149     groups = {cracky = 2, technic_machine = 1, technic_lv = 1, not_in_creative_inventory = 1},
150     connect_sides = {"front", "back", "left", "right", "top"},
151     can_dig = technic.machine_can_dig,
152     technic_run = lamp_run,
153     technic_on_disable = function(pos)
154         illuminate(pos, false)
155         technic.swap_node(pos, "technic:lv_lamp")
156     end,
157     on_destruct = illuminate,
158     on_rightclick = lamp_toggle,
159 })
160
161 technic.register_machine("LV", "technic:lv_lamp", technic.receiver)
162 technic.register_machine("LV", "technic:lv_lamp_active", technic.receiver)
163
164 minetest.register_craft({
165     output = "technic:lv_lamp",
166     recipe = {
167         {"default:glass", "default:glass", "default:glass"},
168         {"technic:lv_led", "technic:lv_led", "technic:lv_led"},
169         {"mesecons_materials:glue", "technic:lv_cable", "mesecons_materials:glue"},
170     }
171 })