forked from ac-minetest/basic_machines
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlight.lua
173 lines (150 loc) · 5.21 KB
/
light.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
-- (c) 2015-2016 rnd
-- Copyright (C) 2022-2025 мтест
-- See README.md for license details
local F, S = basic_machines.F, basic_machines.S
local light_on, light_off
if minetest.global_exists("unifieddyes") then
light_on = {
groups = {cracky = 3, ud_param2_colorable = 1},
palette = "unifieddyes_palette_colorwallmounted.png",
paramtype2 = "color",
on_blast = function(pos, intensity)
local param2 = minetest.get_node(pos).param2
return basic_machines.on_blast(pos, intensity, "basic_machines:light_on", nil, param2)
end,
action_on = function(pos, _)
local meta = minetest.get_meta(pos)
if meta:get_float("deactivate") == 99 then
local node = minetest.get_node_or_nil(pos)
if node and node.name == "basic_machines:light_on" then
node.param2 = node.param2 + 8; if node.param2 > 248 then node.param2 = 0 end
minetest.swap_node(pos, node)
end
else
local count = tonumber(meta:get_string("infotext")) or 0
meta:set_string("infotext", count + 1) -- increase activate count
end
end,
action_off = function(pos, _)
local node = minetest.get_node_or_nil(pos)
if node and node.name == "basic_machines:light_on" then
minetest.swap_node(pos, {name = "basic_machines:light_off", param2 = node.param2})
end
end
}
light_off = {
groups = {cracky = 3, not_in_creative_inventory = 1, ud_param2_colorable = 1},
palette = "unifieddyes_palette_colorwallmounted.png",
paramtype2 = "color",
on_blast = function(pos, intensity)
local param2 = minetest.get_node(pos).param2
return basic_machines.on_blast(pos, intensity, "basic_machines:light_off", nil, param2)
end,
action_on = function(pos, _)
local node = minetest.get_node_or_nil(pos)
if node and node.name == "basic_machines:light_off" then
local deactivate = minetest.get_meta(pos):get_float("deactivate")
if deactivate == 99 then
node.param2 = node.param2 + 8; if node.param2 > 248 then node.param2 = 0 end
minetest.swap_node(pos, node); return
end
minetest.swap_node(pos, {name = "basic_machines:light_on", param2 = node.param2})
if deactivate > 0 then
minetest.after(deactivate, function()
minetest.swap_node(pos, node) -- turn off again
end)
end
end
end
}
else
light_on = {
groups = {cracky = 3},
on_blast = function(pos, intensity)
return basic_machines.on_blast(pos, intensity, "basic_machines:light_on")
end,
action_on = function(pos, _)
local meta = minetest.get_meta(pos)
local count = tonumber(meta:get_string("infotext")) or 0
meta:set_string("infotext", count + 1) -- increase activate count
end,
action_off = function(pos, _)
minetest.swap_node(pos, {name = "basic_machines:light_off"})
end
}
light_off = {
groups = {cracky = 3, not_in_creative_inventory = 1},
on_blast = function(pos, intensity)
return basic_machines.on_blast(pos, intensity, "basic_machines:light_off")
end,
action_on = function(pos, _)
minetest.swap_node(pos, {name = "basic_machines:light_on"})
local deactivate = minetest.get_meta(pos):get_float("deactivate")
if deactivate > 0 then
minetest.after(deactivate, function()
minetest.swap_node(pos, {name = "basic_machines:light_off"}) -- turn off again
end)
end
end
}
end
-- LIGHT ON
minetest.register_node("basic_machines:light_on", {
description = S("Light"),
groups = light_on.groups,
light_source = 14,
tiles = {"basic_machines_light.png"},
paramtype2 = light_on.paramtype2,
palette = light_on.palette,
is_ground_content = false,
sounds = basic_machines.sound_node_machine(),
after_place_node = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "formspec_version[4]size[2.5,2.4]" ..
"field[0.25,0.4;2,0.8;deactivate;" .. F(S("Deactivate after:")) .. ";0" ..
"]button_exit[0.25,1.35;1,0.8;OK;" .. F(S("OK")) .. "]")
meta:set_float("deactivate", 0)
end,
on_receive_fields = function(pos, _, fields, sender)
if fields.OK then
if minetest.is_protected(pos, sender:get_player_name()) then return end
local deactivate = tonumber(fields.deactivate) or 0
if deactivate >= 0 and deactivate <= 600 then
local meta = minetest.get_meta(pos)
deactivate = basic_machines.twodigits_float(deactivate)
meta:set_string("formspec", "formspec_version[4]size[2.5,2.5]" ..
"field[0.25,0.5;2,0.8;deactivate;" .. F(S("Deactivate after:")) .. ";" .. deactivate ..
"]button_exit[0.25,1.45;1,0.8;OK;" .. F(S("OK")) .. "]")
meta:set_float("deactivate", deactivate)
end
end
end,
on_blast = light_on.on_blast,
effector = {
action_on = light_on.action_on,
action_off = light_on.action_off
}
})
-- LIGHT OFF
minetest.register_node("basic_machines:light_off", {
description = S("Light off"),
groups = light_off.groups,
tiles = {"basic_machines_light_off.png"},
paramtype2 = light_off.paramtype2,
palette = light_off.palette,
is_ground_content = false,
sounds = basic_machines.sound_node_machine(),
on_blast = light_off.on_blast,
effector = {
action_on = light_off.action_on
}
})
if basic_machines.settings.register_crafts and basic_machines.use_default then
minetest.register_craft({
output = "basic_machines:light_on",
recipe = {
{"default:torch", "default:torch"},
{"default:torch", "default:torch"}
}
})
end