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