BobFred7
2020-05-13 df7f2e464afa034fa5667f0ba7c0e391a8e793fe
commit | author | age
aa8af0 1
N 2 local S = technic.getter
3
5d05f5 4 local fs_helpers = pipeworks.fs_helpers
ca8655 5 local tube_entry = "^pipeworks_tube_connection_metallic.png"
5d05f5 6
1a45ad 7 function technic.default_can_insert(pos, node, stack, direction)
SZ 8     local meta = minetest.get_meta(pos)
9     local inv = meta:get_inventory()
10     if meta:get_int("splitstacks") == 1 then
11         stack = stack:peek_item(1)
12     end
13     return inv:room_for_item("src", stack)
14 end
15
16 function technic.new_default_tube()
17     return {
18         insert_object = function(pos, node, stack, direction)
19             local meta = minetest.get_meta(pos)
20             local inv = meta:get_inventory()
21             return inv:add_item("src", stack)
22         end,
23         can_insert = technic.default_can_insert,
24         connect_sides = {left = 1, right = 1, back = 1, top = 1, bottom = 1},
25     }
26 end
aa8af0 27
83c649 28 local connect_default = {"bottom", "back", "left", "right"}
S 29
814646 30 local function round(v)
Z 31     return math.floor(v + 0.5)
32 end
33
aa8af0 34 function technic.register_base_machine(data)
N 35     local typename = data.typename
dd65a6 36     local input_size = technic.recipes[typename].input_size
aa8af0 37     local machine_name = data.machine_name
N 38     local machine_desc = data.machine_desc
39     local tier = data.tier
40     local ltier = string.lower(tier)
41
83c649 42     local groups = {cracky = 2, technic_machine = 1, ["technic_"..ltier] = 1}
aa8af0 43     if data.tube then
N 44         groups.tubedevice = 1
45         groups.tubedevice_receiver = 1
46     end
83c649 47     local active_groups = {not_in_creative_inventory = 1}
S 48     for k, v in pairs(groups) do active_groups[k] = v end
aa8af0 49
N 50     local formspec =
fb9338 51         "size[8,9;]"..
dd65a6 52         "list[current_name;src;"..(4-input_size)..",1;"..input_size..",1;]"..
aa8af0 53         "list[current_name;dst;5,1;2,2;]"..
N 54         "list[current_player;main;0,5;8,4;]"..
d732c8 55         "label[0,0;"..machine_desc:format(tier).."]"..
E 56         "listring[current_name;dst]"..
57         "listring[current_player;main]"..
58         "listring[current_name;src]"..
59         "listring[current_player;main]"
aa8af0 60     if data.upgrade then
N 61         formspec = formspec..
62             "list[current_name;upgrade1;1,3;1,1;]"..
63             "list[current_name;upgrade2;2,3;1,1;]"..
d732c8 64             "label[1,4;"..S("Upgrade Slots").."]"..
E 65             "listring[current_name;upgrade1]"..
66             "listring[current_player;main]"..
67             "listring[current_name;upgrade2]"..
68             "listring[current_player;main]"
1a45ad 69     end
SZ 70
71     local tube = technic.new_default_tube()
72     if data.can_insert then
73         tube.can_insert = data.can_insert
74     end
75     if data.insert_object then
76         tube.insert_object = data.insert_object
aa8af0 77     end
N 78
563a4c 79     local run = function(pos, node)
N 80         local meta     = minetest.get_meta(pos)
81         local inv      = meta:get_inventory()
82         local eu_input = meta:get_int(tier.."_EU_input")
83
84         local machine_desc_tier = machine_desc:format(tier)
85         local machine_node      = "technic:"..ltier.."_"..machine_name
86         local machine_demand    = data.demand
87
88         -- Setup meta data if it does not exist.
89         if not eu_input then
90             meta:set_int(tier.."_EU_demand", machine_demand[1])
91             meta:set_int(tier.."_EU_input", 0)
92             return
93         end
94
95         local EU_upgrade, tube_upgrade = 0, 0
96         if data.upgrade then
97             EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
98         end
99         if data.tube then
100             technic.handle_machine_pipeworks(pos, tube_upgrade)
101         end
102
814646 103         local powered = eu_input >= machine_demand[EU_upgrade+1]
Z 104         if powered then
105             meta:set_int("src_time", meta:get_int("src_time") + round(data.speed*10))
563a4c 106         end
814646 107         while true do
Z 108             local result = technic.get_recipe(typename, inv:get_list("src"))
109             if not result then
110                 technic.swap_node(pos, machine_node)
111                 meta:set_string("infotext", S("%s Idle"):format(machine_desc_tier))
112                 meta:set_int(tier.."_EU_demand", 0)
113                 meta:set_int("src_time", 0)
114                 return
115             end
116             meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1])
563a4c 117             technic.swap_node(pos, machine_node.."_active")
N 118             meta:set_string("infotext", S("%s Active"):format(machine_desc_tier))
814646 119             if meta:get_int("src_time") < round(result.time*10) then
Z 120                 if not powered then
121                     technic.swap_node(pos, machine_node)
122                     meta:set_string("infotext", S("%s Unpowered"):format(machine_desc_tier))
dd65a6 123                 end
814646 124                 return
563a4c 125             end
814646 126             local output = result.output
Z 127             if type(output) ~= "table" then output = { output } end
128             local output_stacks = {}
129             for _, o in ipairs(output) do
130                 table.insert(output_stacks, ItemStack(o))
131             end
132             local room_for_output = true
133             inv:set_size("dst_tmp", inv:get_size("dst"))
134             inv:set_list("dst_tmp", inv:get_list("dst"))
135             for _, o in ipairs(output_stacks) do
136                 if not inv:room_for_item("dst_tmp", o) then
137                     room_for_output = false
138                     break
139                 end
140                 inv:add_item("dst_tmp", o)
141             end
142             if not room_for_output then
a529ba 143                 technic.swap_node(pos, machine_node)
T 144                 meta:set_string("infotext", S("%s Idle"):format(machine_desc_tier))
145                 meta:set_int(tier.."_EU_demand", 0)
814646 146                 meta:set_int("src_time", round(result.time*10))
Z 147                 return
148             end
149             meta:set_int("src_time", meta:get_int("src_time") - round(result.time*10))
150             inv:set_list("src", result.new_input)
151             inv:set_list("dst", inv:get_list("dst_tmp"))
563a4c 152         end
N 153     end
5d05f5 154
ca8655 155     local tentry = tube_entry
VE 156     if ltier == "lv" then
157         tentry = ""
158     end
1a45ad 159
aa8af0 160     minetest.register_node("technic:"..ltier.."_"..machine_name, {
N 161         description = machine_desc:format(tier),
ca8655 162         tiles = {
4f78a6 163             "technic_"..ltier.."_"..machine_name.."_top.png"..tentry,
ca8655 164             "technic_"..ltier.."_"..machine_name.."_bottom.png"..tentry,
VE 165             "technic_"..ltier.."_"..machine_name.."_side.png"..tentry,
166             "technic_"..ltier.."_"..machine_name.."_side.png"..tentry,
167             "technic_"..ltier.."_"..machine_name.."_side.png"..tentry,
5d05f5 168             "technic_"..ltier.."_"..machine_name.."_front.png"
VE 169         },
aa8af0 170         paramtype2 = "facedir",
N 171         groups = groups,
172         tube = data.tube and tube or nil,
83c649 173         connect_sides = data.connect_sides or connect_default,
aa8af0 174         legacy_facedir_simple = true,
N 175         sounds = default.node_sound_wood_defaults(),
176         on_construct = function(pos)
177             local node = minetest.get_node(pos)
178             local meta = minetest.get_meta(pos)
5d05f5 179
VE 180             local form_buttons = ""
181             if not string.find(node.name, ":lv_") then
182                 form_buttons = fs_helpers.cycling_button(
183                     meta,
fab2c4 184                     pipeworks.button_base,
5d05f5 185                     "splitstacks",
VE 186                     {
fab2c4 187                         pipeworks.button_off,
VE 188                         pipeworks.button_on
5d05f5 189                     }
fab2c4 190                 )..pipeworks.button_label
5d05f5 191             end
VE 192
aa8af0 193             meta:set_string("infotext", machine_desc:format(tier))
N 194             meta:set_int("tube_time",  0)
5d05f5 195             meta:set_string("formspec", formspec..form_buttons)
aa8af0 196             local inv = meta:get_inventory()
dd65a6 197             inv:set_size("src", input_size)
aa8af0 198             inv:set_size("dst", 4)
N 199             inv:set_size("upgrade1", 1)
200             inv:set_size("upgrade2", 1)
201         end,
202         can_dig = technic.machine_can_dig,
203         allow_metadata_inventory_put = technic.machine_inventory_put,
204         allow_metadata_inventory_take = technic.machine_inventory_take,
205         allow_metadata_inventory_move = technic.machine_inventory_move,
563a4c 206         technic_run = run,
011397 207         after_place_node = data.tube and pipeworks.after_place,
5d05f5 208         after_dig_node = technic.machine_after_dig_node,
VE 209         on_receive_fields = function(pos, formname, fields, sender)
210             local node = minetest.get_node(pos)
211             if not pipeworks.may_configure(pos, sender) then return end
212             fs_helpers.on_receive_fields(pos, fields)
213             local meta = minetest.get_meta(pos)
214             local form_buttons = ""
215             if not string.find(node.name, ":lv_") then
216                 form_buttons = fs_helpers.cycling_button(
217                     meta,
fab2c4 218                     pipeworks.button_base,
5d05f5 219                     "splitstacks",
VE 220                     {
fab2c4 221                         pipeworks.button_off,
VE 222                         pipeworks.button_on
5d05f5 223                     }
fab2c4 224                 )..pipeworks.button_label
5d05f5 225             end
VE 226             meta:set_string("formspec", formspec..form_buttons)
227         end,
aa8af0 228     })
N 229
230     minetest.register_node("technic:"..ltier.."_"..machine_name.."_active",{
231         description = machine_desc:format(tier),
ca8655 232         tiles = {
VE 233             "technic_"..ltier.."_"..machine_name.."_top.png"..tentry,
234             "technic_"..ltier.."_"..machine_name.."_bottom.png"..tentry,
235             "technic_"..ltier.."_"..machine_name.."_side.png"..tentry,
236             "technic_"..ltier.."_"..machine_name.."_side.png"..tentry,
237             "technic_"..ltier.."_"..machine_name.."_side.png"..tentry,
5d05f5 238             "technic_"..ltier.."_"..machine_name.."_front_active.png"
VE 239         },
aa8af0 240         paramtype2 = "facedir",
N 241         drop = "technic:"..ltier.."_"..machine_name,
242         groups = active_groups,
83c649 243         connect_sides = data.connect_sides or connect_default,
aa8af0 244         legacy_facedir_simple = true,
N 245         sounds = default.node_sound_wood_defaults(),
246         tube = data.tube and tube or nil,
247         can_dig = technic.machine_can_dig,
248         allow_metadata_inventory_put = technic.machine_inventory_put,
249         allow_metadata_inventory_take = technic.machine_inventory_take,
250         allow_metadata_inventory_move = technic.machine_inventory_move,
563a4c 251         technic_run = run,
N 252         technic_disabled_machine_name = "technic:"..ltier.."_"..machine_name,
5d05f5 253         on_receive_fields = function(pos, formname, fields, sender)
VE 254             local node = minetest.get_node(pos)
255             if not pipeworks.may_configure(pos, sender) then return end
256             fs_helpers.on_receive_fields(pos, fields)
257             local meta = minetest.get_meta(pos)
258             local form_buttons = ""
259             if not string.find(node.name, ":lv_") then
260                 form_buttons = fs_helpers.cycling_button(
261                     meta,
fab2c4 262                     pipeworks.button_base,
5d05f5 263                     "splitstacks",
VE 264                     {
fab2c4 265                         pipeworks.button_off,
VE 266                         pipeworks.button_on
5d05f5 267                     }
fab2c4 268                 )..pipeworks.button_label
5d05f5 269             end
VE 270             meta:set_string("formspec", formspec..form_buttons)
271         end,
aa8af0 272     })
N 273
274     technic.register_machine(tier, "technic:"..ltier.."_"..machine_name,            technic.receiver)
275     technic.register_machine(tier, "technic:"..ltier.."_"..machine_name.."_active", technic.receiver)
276
277 end -- End registration
278