]> git.lizzy.rs Git - Crafter.git/blob - mods/hopper/abms.lua
Add in TenPlus1's hopper
[Crafter.git] / mods / hopper / abms.lua
1
2 -- suck in items on top of hopper
3 minetest.register_abm({
4         label = "Hopper suction",
5         nodenames = {"hopper:hopper", "hopper:hopper_side"},
6         interval = 0.1,
7         chance = 1,
8         action = function(pos, node, active_object_count, active_object_count_wider)
9                 if active_object_count_wider == 0 then
10                         return
11                 end
12                 
13                 local inv = minetest.get_meta(pos):get_inventory()
14                 local posob
15
16                 for _,object in pairs(minetest.get_objects_inside_radius(pos, 1)) do
17                         if not object:is_player()
18                         and object:get_luaentity()
19                         and object:get_luaentity().name == "__builtin:item"
20                         and inv
21                         and inv:room_for_item("main",
22                                 ItemStack(object:get_luaentity().itemstring)) then
23
24                                 posob = object:getpos()
25
26                                 if math.abs(posob.x - pos.x) <= 0.5
27                                 and posob.y - pos.y <= 0.85
28                                 and posob.y - pos.y >= 0.3 then
29
30                                         inv:add_item("main",
31                                                 ItemStack(object:get_luaentity().itemstring))
32
33                                         object:get_luaentity().itemstring = ""
34                                         object:remove()
35                                 end
36                         end
37                 end
38         end,
39 })
40
41 -- Used to convert side hopper facing into source and destination relative coordinates
42 -- This was tedious to populate and test
43 local directions = {
44         [0]={["src"]={x=0, y=1, z=0},["dst"]={x=-1, y=0, z=0}},
45         [1]={["src"]={x=0, y=1, z=0},["dst"]={x=0, y=0, z=1}},
46         [2]={["src"]={x=0, y=1, z=0},["dst"]={x=1, y=0, z=0}},
47         [3]={["src"]={x=0, y=1, z=0},["dst"]={x=0, y=0, z=-1}},
48         [4]={["src"]={x=0, y=0, z=1},["dst"]={x=-1, y=0, z=0}},
49         [5]={["src"]={x=0, y=0, z=1},["dst"]={x=0, y=-1, z=0}},
50         [6]={["src"]={x=0, y=0, z=1},["dst"]={x=1, y=0, z=0}},
51         [7]={["src"]={x=0, y=0, z=1},["dst"]={x=0, y=1, z=0}},
52         [8]={["src"]={x=0, y=0, z=-1},["dst"]={x=-1, y=0, z=0}},
53         [9]={["src"]={x=0, y=0, z=-1},["dst"]={x=0, y=1, z=0}},
54         [10]={["src"]={x=0, y=0, z=-1},["dst"]={x=1, y=0, z=0}},
55         [11]={["src"]={x=0, y=0, z=-1},["dst"]={x=0, y=-1, z=0}},
56         [12]={["src"]={x=1, y=0, z=0},["dst"]={x=0, y=1, z=0}},
57         [13]={["src"]={x=1, y=0, z=0},["dst"]={x=0, y=0, z=1}},
58         [14]={["src"]={x=1, y=0, z=0},["dst"]={x=0, y=-1, z=0}},
59         [15]={["src"]={x=1, y=0, z=0},["dst"]={x=0, y=0, z=-1}},
60         [16]={["src"]={x=-1, y=0, z=0},["dst"]={x=0, y=-1, z=0}},
61         [17]={["src"]={x=-1, y=0, z=0},["dst"]={x=0, y=0, z=1}},
62         [18]={["src"]={x=-1, y=0, z=0},["dst"]={x=0, y=1, z=0}},
63         [19]={["src"]={x=-1, y=0, z=0},["dst"]={x=0, y=0, z=-1}},
64         [20]={["src"]={x=0, y=-1, z=0},["dst"]={x=1, y=0, z=0}},
65         [21]={["src"]={x=0, y=-1, z=0},["dst"]={x=0, y=0, z=1}},
66         [22]={["src"]={x=0, y=-1, z=0},["dst"]={x=-1, y=0, z=0}},
67         [23]={["src"]={x=0, y=-1, z=0},["dst"]={x=0, y=0, z=-1}},
68 }
69
70 local bottomdir = function(facedir)
71         return ({[0]={x=0, y=-1, z=0},
72                 {x=0, y=0, z=-1},
73                 {x=0, y=0, z=1},
74                 {x=-1, y=0, z=0},
75                 {x=1, y=0, z=0},
76                 {x=0, y=1, z=0}})[math.floor(facedir/4)]
77 end
78
79 -- hopper workings
80 minetest.register_abm({
81         label = "Hopper transfer",
82         nodenames = {"hopper:hopper", "hopper:hopper_side"},
83         neighbors = hopper.neighbors,
84         interval = 0.1,
85         chance = 1,
86         --catch_up = false,
87
88         action = function(pos, node, active_object_count, active_object_count_wider)
89                 local source_pos, destination_pos, destination_dir
90                 if node.name == "hopper:hopper_side" then
91                         source_pos = vector.add(pos, directions[node.param2].src)
92                         destination_dir = directions[node.param2].dst
93                         destination_pos = vector.add(pos, destination_dir)
94                 else
95                         destination_dir = bottomdir(node.param2)
96                         source_pos = vector.subtract(pos, destination_dir)
97                         destination_pos = vector.add(pos, destination_dir)
98                 end
99                 
100                 local output_direction
101                 if destination_dir.y == 0 then
102                         output_direction = "horizontal"
103                 end
104                 
105                 local source_node = minetest.get_node(source_pos)
106                 local destination_node = minetest.get_node(destination_pos)
107
108                 local registered_source_inventories = hopper.get_registered_inventories_for(source_node.name)
109                 if registered_source_inventories ~= nil then
110                         hopper.take_item_from(pos, source_pos, source_node, registered_source_inventories["top"])
111                 end
112                 
113                 local registered_destination_inventories = hopper.get_registered_inventories_for(destination_node.name)
114                 if registered_destination_inventories ~= nil then
115                         if output_direction == "horizontal" then
116                                 hopper.send_item_to(pos, destination_pos, destination_node, registered_destination_inventories["side"])
117                         else
118                                 hopper.send_item_to(pos, destination_pos, destination_node, registered_destination_inventories["bottom"])
119                         end
120                 else
121                         hopper.send_item_to(pos, destination_pos, destination_node) -- for handling ejection
122                 end
123         end,
124 })