]> git.lizzy.rs Git - Crafter.git/blob - mods/redstone/player_detector.lua
1310c4a9683de6af240065c2e8c08224a00e2031
[Crafter.git] / mods / redstone / player_detector.lua
1 local minetest,ipairs,math = minetest,ipairs,math
2 --detects players and outputs accordingly
3 for i = 0,9  do
4 minetest.register_node("redstone:player_detector_"..i, {
5         description = "Redstone Player Detector",
6         --inventory_image = "redstone_torch.png",
7         --wield_image = "redstone_torch.png",
8         --wield_scale = {x = 1, y = 1, z = 1 + 2/16},
9         drawtype = "normal",
10         tiles = {"player_detector.png"},
11         paramtype = "light",
12         paramtype2 = "none",
13         power = 9,
14         --sunlight_propagates = true,
15         drop = "redstone:player_detector_0",
16         --walkable = false,
17         light_source = i,
18         groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4, torch=1,redstone=1,redstone_torch=1,redstone_power=i, redstone_player_detection = 1},
19         legacy_wallmounted = true,
20         
21         on_construct = function(pos)
22                 redstone.collect_info(pos)
23         end,
24         after_destruct = function(pos, oldnode)
25                 redstone.collect_info(pos)
26         end,
27         sounds = main.stoneSound(),
28 })
29 end
30
31 minetest.register_abm{
32     label = "Redstone Player Detection",
33         nodenames = {"group:redstone_player_detection"},
34         --neighbors = {"group:water"},
35         interval = 0.2,
36         chance = 1,
37         action = function(pos)
38                 local found_player = false
39                 for _,object in ipairs(minetest.get_objects_inside_radius(pos, 9)) do
40                         if object:is_player() and object:get_hp() > 0 then
41                                 local level = minetest.get_item_group(minetest.get_node(pos).name, "redstone_power")
42                                 found_player = true
43                                 local pos2 = object:get_pos()
44                                 pos2 = vector.floor(vector.add(pos2,0.5))
45                                 local distance = math.floor(vector.distance(pos2,pos))
46                                 distance = math.abs(distance - 9)
47                                 --print(distance)
48                                 if level ~= distance then
49                                         minetest.set_node(pos,{name="redstone:player_detector_"..distance})
50                                         redstone.collect_info(pos)
51                                         --print(distance)
52                                 end
53                         end
54                 end
55                 if found_player == false then
56                         minetest.set_node(pos,{name="redstone:player_detector_0"})
57                         redstone.collect_info(pos)
58                 end
59         end,
60 }