Skip to content

Commit 6b54f02

Browse files
committedFeb 14, 2016
Merge branch 'playerdetector_update' of https://github.com/HybridDog/minetest-mod-mesecons into HybridDog-playerdetector_update
2 parents c98805a + c1d0481 commit 6b54f02

File tree

1 file changed

+102
-108
lines changed

1 file changed

+102
-108
lines changed
 

‎mesecons_detector/init.lua

+102-108
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ local GET_COMMAND = "GET"
44
-- Detects players in a certain radius
55
-- The radius can be specified in mesecons/settings.lua
66

7-
local object_detector_make_formspec = function (pos)
8-
local meta = minetest.get_meta(pos)
9-
meta:set_string("formspec", "size[9,2.5]" ..
7+
local function object_detector_make_formspec(pos)
8+
minetest.get_meta(pos):set_string("formspec", "size[9,2.5]" ..
109
"field[0.3, 0;9,2;scanname;Name of player to scan for (empty for any):;${scanname}]"..
1110
"field[0.3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]"..
1211
"button_exit[7,0.75;2,3;;Save]")
1312
end
1413

15-
local object_detector_on_receive_fields = function(pos, formname, fields)
16-
if not fields.scanname or not fields.digiline_channel then return end;
14+
local function object_detector_on_receive_fields(pos, _, fields)
15+
if not fields.scanname or not fields.digiline_channel then return end
1716

1817
local meta = minetest.get_meta(pos)
1918
meta:set_string("scanname", fields.scanname)
@@ -22,25 +21,35 @@ local object_detector_on_receive_fields = function(pos, formname, fields)
2221
end
2322

2423
-- returns true if player was found, false if not
25-
local object_detector_scan = function (pos)
24+
local function object_detector_scan(pos)
2625
local objs = minetest.get_objects_inside_radius(pos, mesecon.setting("detector_radius", 6))
27-
for k, obj in pairs(objs) do
28-
local isname = obj:get_player_name() -- "" is returned if it is not a player; "" ~= nil!
29-
local scanname = minetest.get_meta(pos):get_string("scanname")
30-
if (isname == scanname and isname ~= "") or (isname ~= "" and scanname == "") then -- player with scanname found or not scanname specified
31-
return true
26+
27+
-- abort if no scan results were found
28+
if next(objs) == nil then return false end
29+
30+
local scanname = minetest.get_meta(pos):get_string("scanname")
31+
local every_player = scanname == ""
32+
for _, obj in pairs(objs) do
33+
-- "" is returned if it is not a player; "" ~= nil; so only handle objects with foundname ~= ""
34+
local foundname = obj:get_player_name()
35+
36+
if foundname ~= "" then
37+
-- return true if scanning for any player or if specific playername was detected
38+
if scanname == "" or foundname == scanname then
39+
return true
40+
end
3241
end
3342
end
43+
3444
return false
3545
end
3646

3747
-- set player name when receiving a digiline signal on a specific channel
3848
local object_detector_digiline = {
3949
effector = {
40-
action = function (pos, node, channel, msg)
50+
action = function(pos, node, channel, msg)
4151
local meta = minetest.get_meta(pos)
42-
local active_channel = meta:get_string("digiline_channel")
43-
if channel == active_channel then
52+
if channel == meta:get_string("digiline_channel") then
4453
meta:set_string("scanname", msg)
4554
object_detector_make_formspec(pos)
4655
end
@@ -89,85 +98,104 @@ minetest.register_craft({
8998
}
9099
})
91100

92-
minetest.register_abm(
93-
{nodenames = {"mesecons_detector:object_detector_off"},
94-
interval = 1.0,
101+
minetest.register_abm({
102+
nodenames = {"mesecons_detector:object_detector_off"},
103+
interval = 1,
95104
chance = 1,
96-
action = function(pos)
97-
if object_detector_scan(pos) then
98-
minetest.swap_node(pos, {name = "mesecons_detector:object_detector_on"})
99-
mesecon.receptor_on(pos, mesecon.rules.pplate)
100-
end
105+
action = function(pos, node)
106+
if not object_detector_scan(pos) then return end
107+
108+
node.name = "mesecons_detector:object_detector_on"
109+
minetest.swap_node(pos, node)
110+
mesecon.receptor_on(pos, mesecon.rules.pplate)
101111
end,
102112
})
103113

104-
minetest.register_abm(
105-
{nodenames = {"mesecons_detector:object_detector_on"},
106-
interval = 1.0,
114+
minetest.register_abm({
115+
nodenames = {"mesecons_detector:object_detector_on"},
116+
interval = 1,
107117
chance = 1,
108-
action = function(pos)
109-
if not object_detector_scan(pos) then
110-
minetest.swap_node(pos, {name = "mesecons_detector:object_detector_off"})
111-
mesecon.receptor_off(pos, mesecon.rules.pplate)
112-
end
118+
action = function(pos, node)
119+
if object_detector_scan(pos) then return end
120+
121+
node.name = "mesecons_detector:object_detector_off"
122+
minetest.swap_node(pos, node)
123+
mesecon.receptor_off(pos, mesecon.rules.pplate)
113124
end,
114125
})
115126

116127
-- Node detector
117128
-- Detects the node in front of it
118129

119-
local node_detector_make_formspec = function (pos)
120-
local meta = minetest.get_meta(pos)
121-
meta:set_string("formspec", "size[9,2.5]" ..
130+
local function node_detector_make_formspec(pos)
131+
minetest.get_meta(pos):set_string("formspec", "size[9,2.5]" ..
122132
"field[0.3, 0;9,2;scanname;Name of node to scan for (empty for any):;${scanname}]"..
123133
"field[0.3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]"..
124134
"button_exit[7,0.75;2,3;;Save]")
125135
end
126136

127-
local node_detector_on_receive_fields = function(pos, formname, fields)
128-
if not fields.scanname or not fields.digiline_channel then return end;
137+
local function node_detector_on_receive_fields(pos, _, fields)
138+
if not fields.scanname or not fields.digiline_channel then return end
129139

130140
local meta = minetest.get_meta(pos)
131141
meta:set_string("scanname", fields.scanname)
132142
meta:set_string("digiline_channel", fields.digiline_channel)
133143
node_detector_make_formspec(pos)
134144
end
135145

136-
-- returns true if player was found, false if not
137-
local node_detector_scan = function (pos)
138-
if not pos then return end
146+
-- returns true if node was found, false if not
147+
local function node_detector_scan(pos)
139148
local node = minetest.get_node_or_nil(pos)
140149
if not node then return end
141-
local scandir = minetest.facedir_to_dir(node.param2)
142-
if not scandir then return end
143-
local frontpos = vector.subtract(pos, scandir)
144-
local frontnode = minetest.get_node(frontpos)
145-
local meta = minetest.get_meta(pos)
146-
return (frontnode.name == meta:get_string("scanname")) or
147-
(frontnode.name ~= "air" and frontnode.name ~= "ignore" and meta:get_string("scanname") == "")
150+
151+
local frontname = minetest.get_node(
152+
vector.subtract(pos, minetest.facedir_to_dir(node.param2))
153+
).name
154+
local scanname = minetest.get_meta(pos):get_string("scanname")
155+
156+
return (frontname == scanname) or
157+
(frontname ~= "air" and frontname ~= "ignore" and scanname == "")
148158
end
149159

150160
-- set player name when receiving a digiline signal on a specific channel
151161
local node_detector_digiline = {
152162
effector = {
153-
action = function (pos, node, channel, msg)
163+
action = function(pos, node, channel, msg)
154164
local meta = minetest.get_meta(pos)
155-
local active_channel = meta:get_string("digiline_channel")
156-
if channel == active_channel then
157-
if msg == GET_COMMAND then
158-
local frontpos = vector.subtract(pos, minetest.facedir_to_dir(node.param2))
159-
local name = minetest.get_node(frontpos).name
160-
digiline:receptor_send(pos, digiline.rules.default, channel, name)
161-
else
162-
meta:set_string("scanname", msg)
163-
node_detector_make_formspec(pos)
164-
end
165+
if channel ~= meta:get_string("digiline_channel") then return end
166+
167+
if msg == GET_COMMAND then
168+
local nodename = minetest.get_node(
169+
vector.subtract(pos, minetest.facedir_to_dir(node.param2))
170+
).name
171+
172+
digiline:receptor_send(pos, digiline.rules.default, channel, nodename)
173+
else
174+
meta:set_string("scanname", msg)
175+
node_detector_make_formspec(pos)
165176
end
166177
end,
167178
},
168179
receptor = {}
169180
}
170181

182+
local function after_place_node_detector(pos, placer)
183+
local placer_pos = placer:getpos()
184+
if not placer_pos then
185+
return
186+
end
187+
188+
--correct for the player's height
189+
if placer:is_player() then
190+
placer_pos.y = placer_pos.y + 1.625
191+
end
192+
193+
--correct for 6d facedir
194+
local node = minetest.get_node(pos)
195+
node.param2 = minetest.dir_to_facedir(vector.subtract(pos, placer_pos), true)
196+
minetest.set_node(pos, node)
197+
end
198+
171199
minetest.register_node("mesecons_detector:node_detector_off", {
172200
tiles = {"default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "jeija_node_detector_off.png"},
173201
paramtype = "light",
@@ -180,25 +208,7 @@ minetest.register_node("mesecons_detector:node_detector_off", {
180208
}},
181209
on_construct = node_detector_make_formspec,
182210
on_receive_fields = node_detector_on_receive_fields,
183-
after_place_node = function (pos, placer)
184-
local placer_pos = placer:getpos()
185-
186-
--correct for the player's height
187-
if placer:is_player() then placer_pos.y = placer_pos.y + 1.5 end
188-
189-
--correct for 6d facedir
190-
if placer_pos then
191-
local dir = {
192-
x = pos.x - placer_pos.x,
193-
y = pos.y - placer_pos.y,
194-
z = pos.z - placer_pos.z
195-
}
196-
local node = minetest.get_node(pos)
197-
node.param2 = minetest.dir_to_facedir(dir, true)
198-
minetest.set_node(pos, node)
199-
minetest.log("action", "real (6d) facedir: " .. node.param2)
200-
end
201-
end,
211+
after_place_node = after_place_node_detector,
202212
sounds = default.node_sound_stone_defaults(),
203213
digiline = node_detector_digiline
204214
})
@@ -215,25 +225,7 @@ minetest.register_node("mesecons_detector:node_detector_on", {
215225
}},
216226
on_construct = node_detector_make_formspec,
217227
on_receive_fields = node_detector_on_receive_fields,
218-
after_place_node = function (pos, placer)
219-
local placer_pos = placer:getpos()
220-
221-
--correct for the player's height
222-
if placer:is_player() then placer_pos.y = placer_pos.y + 1.5 end
223-
224-
--correct for 6d facedir
225-
if placer_pos then
226-
local dir = {
227-
x = pos.x - placer_pos.x,
228-
y = pos.y - placer_pos.y,
229-
z = pos.z - placer_pos.z
230-
}
231-
local node = minetest.get_node(pos)
232-
node.param2 = minetest.dir_to_facedir(dir, true)
233-
minetest.set_node(pos, node)
234-
minetest.log("action", "real (6d) facedir: " .. node.param2)
235-
end
236-
end,
228+
after_place_node = after_place_node_detector,
237229
sounds = default.node_sound_stone_defaults(),
238230
digiline = node_detector_digiline
239231
})
@@ -247,26 +239,28 @@ minetest.register_craft({
247239
}
248240
})
249241

250-
minetest.register_abm(
251-
{nodenames = {"mesecons_detector:node_detector_off"},
252-
interval = 1.0,
242+
minetest.register_abm({
243+
nodenames = {"mesecons_detector:node_detector_off"},
244+
interval = 1,
253245
chance = 1,
254246
action = function(pos, node)
255-
if node_detector_scan(pos) then
256-
minetest.swap_node(pos, {name = "mesecons_detector:node_detector_on", param2 = node.param2})
257-
mesecon.receptor_on(pos)
258-
end
247+
if not node_detector_scan(pos) then return end
248+
249+
node.name = "mesecons_detector:node_detector_on"
250+
minetest.swap_node(pos, node)
251+
mesecon.receptor_on(pos)
259252
end,
260253
})
261254

262-
minetest.register_abm(
263-
{nodenames = {"mesecons_detector:node_detector_on"},
264-
interval = 1.0,
255+
minetest.register_abm({
256+
nodenames = {"mesecons_detector:node_detector_on"},
257+
interval = 1,
265258
chance = 1,
266259
action = function(pos, node)
267-
if not node_detector_scan(pos) then
268-
minetest.swap_node(pos, {name = "mesecons_detector:node_detector_off", param2 = node.param2})
269-
mesecon.receptor_off(pos)
270-
end
260+
if node_detector_scan(pos) then return end
261+
262+
node.name = "mesecons_detector:node_detector_off"
263+
minetest.swap_node(pos, node)
264+
mesecon.receptor_off(pos)
271265
end,
272266
})

0 commit comments

Comments
 (0)
Please sign in to comment.