Thomas--S
2018-07-16 0fcc7a14c2d60e22d41b66b9008119171aff1681
commit | author | age
82cba9 1
354ee6 2 local S = technic.getter
S 3
14b30b 4 local function deploy_node(inv, slot_name, pos, node, machine_node)
e65c8b 5     if node.param2 > 3 then return end
14b30b 6     if node.name ~= "air" then
S 7         if node.name == "ignore" or
8            node.name == "default:lava_source" or
9            node.name == "default:lava_flowing" or
10            node.name == "default:water_source" or
11            node.name == "default:water_flowing" then
12             return
13         end
14         local drops = minetest.get_node_drops(node.name, "")
15         local remove_to = false
16         for i, item in ipairs(drops) do
17             if not inv:room_for_item(slot_name, item) then
18                 remove_to = i - 1
19                 break
20             end
21             inv:add_item(slot_name, item)
22         end
23         if remove_to then
24             for i = 1, remove_to do
186f9b 25                 inv:remove_item(slot_name, drops[i])
14b30b 26             end
S 27         else
28             minetest.remove_node(pos)
29         end
30         return
31     end
32     if not inv:is_empty(slot_name) then
33         local stack = inv:get_list(slot_name)[1]
34         local def = stack:get_definition()
35         if def.type == "node" then
36             minetest.set_node(pos, {
37                 name = stack:get_name(),
38                 param2 = machine_node.param2
39             })
40             stack:take_item()
41             inv:set_stack(slot_name, 1, stack)
42         elseif def.type == "craft" then
43             if def.on_place then
44                 -- Use pcall to avoid nil placer errors.
45                 -- TODO: Do without pcall.
46                 local ok, stk = pcall(def.on_place, stack, nil, {
018b24 47                     -- Fake pointed_thing
VE 48                     type = "node",
14b30b 49                     above = pos,
S 50                     under = {x=pos.x, y=pos.y-1, z=pos.z},
018b24 51                 })
14b30b 52                 if ok then
S 53                     inv:set_stack(slot_name, 1, stk or stack)
54                     return
018b24 55                 end
VE 56             end
14b30b 57             minetest.item_place_object(stack, nil, {
S 58                 -- Fake pointed_thing
59                 type = "node",
60                 above = pos,
61                 under = pos,
62             })
63             inv:set_stack(slot_name, 1, nil)
018b24 64         end
VE 65     end
66 end
67
82cba9 68 minetest.register_craft({
R 69     type = "shapeless",
70     output = 'technic:constructor_mk1_off 1',
71     recipe = {'technic:nodebreaker_off', 'technic:deployer_off'},
72
73 })
74 minetest.register_craft({
75     type = "shapeless",
76     output = 'technic:constructor_mk2_off 1',
77     recipe = {'technic:constructor_mk1_off', 'technic:constructor_mk1_off'},
78
79 })
80
81 minetest.register_craft({
82     type = "shapeless",
83     output = 'technic:constructor_mk3_off 1',
84     recipe = {'technic:constructor_mk2_off', 'technic:constructor_mk2_off'},
85
86 })
87
14b30b 88 local function make_on(mark, length)
S 89     return function(pos, node)
ee0765 90         local meta = minetest.get_meta(pos)
82cba9 91         local inv = meta:get_inventory()
14b30b 92         local dir = vector.new()
S 93         if node.param2 == 3 then dir.x = 1 end
94         if node.param2 == 2 then dir.z = 1 end
95         if node.param2 == 1 then dir.x = -1 end
96         if node.param2 == 0 then dir.z = -1 end
82cba9 97
14b30b 98         local place_pos = vector.new(pos)
82cba9 99
14b30b 100         if node.name == "technic:constructor_mk"..mark.."_off" then
S 101             technic.swap_node(pos, "technic:constructor_mk"..mark.."_on")
0fcc7a 102             minetest.check_for_falling(pos)
14b30b 103             for i = 1, length do
S 104                 place_pos = vector.add(place_pos, dir)
105                 local place_node = minetest.get_node(place_pos)
106                 deploy_node(inv, "slot"..i, place_pos, place_node, node)
107             end
108         end
82cba9 109     end
R 110 end
111
14b30b 112 local function make_off(mark)
S 113     return function(pos, node)
114         if node.name == "technic:constructor_mk"..mark.."_on" then
115             technic.swap_node(pos,"technic:constructor_mk"..mark.."_off")
0fcc7a 116             minetest.check_for_falling(pos)
14b30b 117         end
82cba9 118     end
R 119 end
120
121
14b30b 122 local function make_constructor(mark, length)
S 123     minetest.register_node("technic:constructor_mk"..mark.."_off", {
124         description = S("Constructor Mk%d"):format(mark),
125         tiles = {"technic_constructor_mk"..mark.."_top_off.png",
126             "technic_constructor_mk"..mark.."_bottom_off.png",
127             "technic_constructor_mk"..mark.."_side2_off.png",
128             "technic_constructor_mk"..mark.."_side1_off.png",
129             "technic_constructor_back.png",
130             "technic_constructor_front_off.png"},
131         paramtype2 = "facedir",
132         groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, mesecon = 2},
133         mesecons = {effector = {action_on = make_on(mark, length)}},
134         sounds = default.node_sound_stone_defaults(),
135         on_construct = function(pos)
136             local meta = minetest.get_meta(pos)
137             local formspec = "size[8,9;]"..
138                 "label[0,0;"..S("Constructor Mk%d"):format(mark).."]"..
139                 "list[current_player;main;0,5;8,4;]"
140             for i = 1, length do
141                 formspec = formspec
142                     .."label[5,"..(i - 1)..";"..S("Slot %d"):format(i).."]"
143                     .."list[current_name;slot"..i
144                         ..";6,"..(i - 1)..";1,1;]"
145             end
146             meta:set_string("formspec", formspec)
147             meta:set_string("infotext", S("Constructor Mk%d"):format(mark))
148             local inv = meta:get_inventory()
149             for i = 1, length do
150                 inv:set_size("slot"..i, 1)
151             end
152         end,
153         can_dig = function(pos, player)
154             local meta = minetest.get_meta(pos)
155             local inv = meta:get_inventory()
156             for i = 1, length do
157                 if not inv:is_empty("slot"..i) then
158                     return false
159                 end
160             end
161             return true
162         end,
163         allow_metadata_inventory_put = technic.machine_inventory_put,
164         allow_metadata_inventory_take = technic.machine_inventory_take,
165         allow_metadata_inventory_move = technic.machine_inventory_move,
e65c8b 166         on_rotate = screwdriver.rotate_simple
14b30b 167     })
82cba9 168
14b30b 169     minetest.register_node("technic:constructor_mk"..mark.."_on", {
S 170         tiles = {"technic_constructor_mk"..mark.."_top_on.png",
171             "technic_constructor_mk"..mark.."_bottom_on.png",
172             "technic_constructor_mk"..mark.."_side2_on.png",
173             "technic_constructor_mk"..mark.."_side1_on.png",
174             "technic_constructor_back.png",
175             "technic_constructor_front_on.png"},
176         paramtype2 = "facedir",
177         drop = "technic:constructor_mk"..mark.."_off",
178         groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
179             mesecon=2, not_in_creative_inventory=1},
180         mesecons= {effector = {action_off = make_off(mark)}},
181         sounds = default.node_sound_stone_defaults(),
182         allow_metadata_inventory_put = technic.machine_inventory_put,
183         allow_metadata_inventory_take = technic.machine_inventory_take,
184         allow_metadata_inventory_move = technic.machine_inventory_move,
e65c8b 185         on_rotate = false
14b30b 186     })
82cba9 187 end
R 188
14b30b 189 make_constructor(1, 1)
S 190 make_constructor(2, 2)
191 make_constructor(3, 4)
82cba9 192