Move all chests into hades_chests mod
[minetest_hades/hades_revisited.git] / mods / pipeworks / deployer.lua
blob84e309792296e7cf5e76baa72799e5262954fd32
2 --register aliases for when someone had technic installed, but then uninstalled it but not pipeworks
3 minetest.register_alias("technic:deployer_off", "pipeworks:deployer_off")
4 minetest.register_alias("technic:deployer_on", "pipeworks:deployer_on")
6 minetest.register_craft({
7 output = 'pipeworks:deployer_off 1',
8 recipe = {
9 {'group:wood', 'group:chest','group:wood'},
10 {'default:stone', 'mesecons:piston','default:stone'},
11 {'default:stone', 'mesecons:mesecon','default:stone'},
15 local function swap_node(pos, name)
16 local node = minetest.get_node(pos)
17 if node.name == name then
18 return
19 end
20 node.name = name
21 minetest.swap_node(pos, node)
22 end
24 local function delay(x)
25 return (function() return x end)
26 end
28 local function deployer_on(pos, node)
29 if node.name ~= "pipeworks:deployer_off" then
30 return
31 end
33 --locate the above and under positions
34 local dir = minetest.facedir_to_dir(node.param2)
35 local pos_under, pos_above = {x = pos.x - dir.x, y = pos.y - dir.y, z = pos.z - dir.z}, {x = pos.x - 2*dir.x, y = pos.y - 2*dir.y, z = pos.z - 2*dir.z}
37 swap_node(pos, "pipeworks:deployer_on")
38 minetest.check_for_falling(pos)
40 local meta = minetest.get_meta(pos)
41 local inv = meta:get_inventory()
42 local invlist = inv:get_list("main")
43 for i, stack in ipairs(invlist) do
44 if stack:get_name() ~= nil and stack:get_name() ~= "" then--and minetest.get_node(pos_under).name == "air" then --obtain the first non-empty item slot
45 local pitch
46 local yaw
47 if dir.z < 0 then
48 yaw = 0
49 pitch = 0
50 elseif dir.z > 0 then
51 yaw = math.pi
52 pitch = 0
53 elseif dir.x < 0 then
54 yaw = 3*math.pi/2
55 pitch = 0
56 elseif dir.x > 0 then
57 yaw = math.pi/2
58 pitch = 0
59 elseif dir.y > 0 then
60 yaw = 0
61 pitch = -math.pi/2
62 else
63 yaw = 0
64 pitch = math.pi/2
65 end
66 local placer = {
67 get_inventory_formspec = delay(meta:get_string("formspec")),
68 get_look_dir = delay({x = -dir.x, y = -dir.y, z = -dir.z}),
69 get_look_pitch = delay(pitch),
70 get_look_yaw = delay(yaw),
71 get_player_control = delay({jump=false, right=false, left=false, LMB=false, RMB=false, sneak=false, aux1=false, down=false, up=false}),
72 get_player_control_bits = delay(0),
73 get_player_name = delay(meta:get_string("owner")),
74 is_player = delay(true),
75 set_inventory_formspec = delay(),
76 getpos = delay({x = pos.x, y = pos.y - 1.5, z = pos.z}), -- Player height
77 get_hp = delay(20),
78 get_inventory = delay(inv),
79 get_wielded_item = delay(stack),
80 get_wield_index = delay(i),
81 get_wield_list = delay("main"),
82 moveto = delay(),
83 punch = delay(),
84 remove = delay(),
85 right_click = delay(),
86 setpos = delay(),
87 set_hp = delay(),
88 set_properties = delay(),
89 set_wielded_item = function(self, item) inv:set_stack("main", i, item) end,
90 set_animation = delay(),
91 set_attach = delay(),
92 set_detach = delay(),
93 set_bone_position = delay(),
95 local stack2 = minetest.item_place(stack, placer, {type="node", under=pos_under, above=pos_above})
96 if minetest.settings:get_bool("creative_mode") and not minetest.get_modpath("unified_inventory") then --infinite stacks ahoy!
97 stack2:take_item()
98 end
99 invlist[i] = stack2
100 inv:set_list("main", invlist)
101 return
106 local deployer_off = function(pos, node)
107 if node.name == "pipeworks:deployer_on" then
108 swap_node(pos, "pipeworks:deployer_off")
109 minetest.check_for_falling(pos)
113 minetest.register_node("pipeworks:deployer_off", {
114 description = "Deployer",
115 tiles = {"pipeworks_deployer_top.png","pipeworks_deployer_bottom.png","pipeworks_deployer_side2.png","pipeworks_deployer_side1.png",
116 "pipeworks_deployer_back.png","pipeworks_deployer_front_off.png"},
117 mesecons = {effector={rules=pipeworks.rules_all,action_on=deployer_on,action_off=deployer_off}},
118 tube={insert_object=function(pos,node,stack,direction)
119 local meta=minetest.get_meta(pos)
120 local inv=meta:get_inventory()
121 return inv:add_item("main",stack)
122 end,
123 can_insert=function(pos,node,stack,direction)
124 local meta=minetest.get_meta(pos)
125 local inv=meta:get_inventory()
126 return inv:room_for_item("main",stack)
127 end,
128 input_inventory="main",
129 connect_sides={back=1}},
130 is_ground_content = true,
131 paramtype2 = "facedir",
132 groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2, mesecon = 2,tubedevice=1, tubedevice_receiver=1},
133 sounds = default.node_sound_stone_defaults(),
134 on_construct = function(pos)
135 local meta = minetest.get_meta(pos)
136 meta:set_string("formspec",
137 "size[8,9;]"..
138 "label[0,0;Deployer]"..
139 "list[current_name;main;4,1;3,3;]"..
140 "list[current_player;main;0,5;8,4;]")
141 meta:set_string("infotext", "Deployer")
142 local inv = meta:get_inventory()
143 inv:set_size("main", 3*3)
144 end,
145 can_dig = function(pos,player)
146 local meta = minetest.get_meta(pos);
147 local inv = meta:get_inventory()
148 return inv:is_empty("main")
149 end,
150 after_place_node = function (pos, placer)
151 pipeworks.scan_for_tube_objects(pos, placer)
152 local placer_pos = placer:get_pos()
154 --correct for the player's height
155 if placer:is_player() then placer_pos.y = placer_pos.y + 1.5 end
157 --correct for 6d facedir
158 if placer_pos then
159 local dir = {
160 x = pos.x - placer_pos.x,
161 y = pos.y - placer_pos.y,
162 z = pos.z - placer_pos.z
164 local node = minetest.get_node(pos)
165 node.param2 = minetest.dir_to_facedir(dir, true)
166 minetest.set_node(pos, node)
167 minetest.log("action", "real (6d) facedir: " .. node.param2)
170 minetest.get_meta(pos):set_string("owner", placer:get_player_name())
171 end,
172 after_dig_node = pipeworks.scan_for_tube_objects,
175 minetest.register_node("pipeworks:deployer_on", {
176 description = "Deployer (on)",
177 tiles = {"pipeworks_deployer_top.png","pipeworks_deployer_bottom.png","pipeworks_deployer_side2.png","pipeworks_deployer_side1.png",
178 "pipeworks_deployer_back.png","pipeworks_deployer_front_on.png"},
179 mesecons = {effector={rules=pipeworks.rules_all,action_on=deployer_on,action_off=deployer_off}},
180 tube={insert_object=function(pos,node,stack,direction)
181 local meta=minetest.get_meta(pos)
182 local inv=meta:get_inventory()
183 return inv:add_item("main",stack)
184 end,
185 can_insert=function(pos,node,stack,direction)
186 local meta=minetest.get_meta(pos)
187 local inv=meta:get_inventory()
188 return inv:room_for_item("main",stack)
189 end,
190 input_inventory="main",
191 connect_sides={back=1}},
192 is_ground_content = true,
193 paramtype2 = "facedir",
194 tubelike=1,
195 drop = "pipeworks:deployer_off",
196 groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2, mesecon = 2,tubedevice=1, tubedevice_receiver=1,not_in_creative_inventory=1},
197 sounds = default.node_sound_stone_defaults(),
198 on_construct = function(pos)
199 local meta = minetest.get_meta(pos)
200 meta:set_string("formspec",
201 "size[8,9;]"..
202 "label[0,0;Deployer]"..
203 "list[current_name;main;4,1;3,3;]"..
204 "list[current_player;main;0,5;8,4;]")
205 meta:set_string("infotext", "Deployer")
206 local inv = meta:get_inventory()
207 inv:set_size("main", 3*3)
208 end,
209 can_dig = function(pos,player)
210 local meta = minetest.get_meta(pos);
211 local inv = meta:get_inventory()
212 return inv:is_empty("main")
213 end,
214 after_place_node = function (pos, placer)
215 pipeworks.scan_for_tube_objects(pos, placer)
216 local placer_pos = placer:get_pos()
218 --correct for the player's height
219 if placer:is_player() then placer_pos.y = placer_pos.y + 1.5 end
221 --correct for 6d facedir
222 if placer_pos then
223 local dir = {
224 x = pos.x - placer_pos.x,
225 y = pos.y - placer_pos.y,
226 z = pos.z - placer_pos.z
228 local node = minetest.get_node(pos)
229 node.param2 = minetest.dir_to_facedir(dir, true)
230 minetest.set_node(pos, node)
231 minetest.log("action", "real (6d) facedir: " .. node.param2)
234 minetest.get_meta(pos):set_string("owner", placer:get_player_name())
235 end,
236 after_dig_node = pipeworks.scan_for_tube_objects,