Maciej 'agaran' Pijanka
2017-03-15 10307f23a78b33af50dc4a5f3d1baafb4ee4b0d9
technic/machines/register/machine_base.lua
@@ -15,6 +15,12 @@
   connect_sides = {left = 1, right = 1, back = 1, top = 1, bottom = 1},
}
local connect_default = {"bottom", "back", "left", "right"}
local function round(v)
   return math.floor(v + 0.5)
end
function technic.register_base_machine(data)
   local typename = data.typename
   local input_size = technic.recipes[typename].input_size
@@ -23,27 +29,33 @@
   local tier = data.tier
   local ltier = string.lower(tier)
   local groups = {cracky = 2, technic_machine = 1}
   local active_groups = {cracky = 2, technic_machine = 1, not_in_creative_inventory = 1}
   local groups = {cracky = 2, technic_machine = 1, ["technic_"..ltier] = 1}
   if data.tube then
      groups.tubedevice = 1
      groups.tubedevice_receiver = 1
      active_groups.tubedevice = 1
      active_groups.tubedevice_receiver = 1
   end
   local active_groups = {not_in_creative_inventory = 1}
   for k, v in pairs(groups) do active_groups[k] = v end
   local formspec =
      "invsize[8,9;]"..
      "list[current_name;src;"..(4-input_size)..",1;"..input_size..",1;]"..
      "list[current_name;dst;5,1;2,2;]"..
      "list[current_player;main;0,5;8,4;]"..
      "label[0,0;"..machine_desc:format(tier).."]"
      "label[0,0;"..machine_desc:format(tier).."]"..
      "listring[current_name;dst]"..
      "listring[current_player;main]"..
      "listring[current_name;src]"..
      "listring[current_player;main]"
   if data.upgrade then
      formspec = formspec..
         "list[current_name;upgrade1;1,3;1,1;]"..
         "list[current_name;upgrade2;2,3;1,1;]"..
         "label[1,4;"..S("Upgrade Slots").."]"
         "label[1,4;"..S("Upgrade Slots").."]"..
         "listring[current_name;upgrade1]"..
         "listring[current_player;main]"..
         "listring[current_name;upgrade2]"..
         "listring[current_player;main]"
   end
   local run = function(pos, node)
@@ -70,51 +82,56 @@
         technic.handle_machine_pipeworks(pos, tube_upgrade)
      end
      local result = technic.get_recipe(typename, inv:get_list("src"))
      if not result then
         technic.swap_node(pos, machine_node)
         meta:set_string("infotext", S("%s Idle"):format(machine_desc_tier))
         meta:set_int(tier.."_EU_demand", 0)
         return
      local powered = eu_input >= machine_demand[EU_upgrade+1]
      if powered then
         meta:set_int("src_time", meta:get_int("src_time") + round(data.speed*10))
      end
      if eu_input < machine_demand[EU_upgrade+1] then
         -- Unpowered - go idle
         technic.swap_node(pos, machine_node)
         meta:set_string("infotext", S("%s Unpowered"):format(machine_desc_tier))
      elseif eu_input >= machine_demand[EU_upgrade+1] then
         -- Powered
      while true do
         local result = technic.get_recipe(typename, inv:get_list("src"))
         if not result then
            technic.swap_node(pos, machine_node)
            meta:set_string("infotext", S("%s Idle"):format(machine_desc_tier))
            meta:set_int(tier.."_EU_demand", 0)
            meta:set_int("src_time", 0)
            return
         end
         meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1])
         technic.swap_node(pos, machine_node.."_active")
         meta:set_string("infotext", S("%s Active"):format(machine_desc_tier))
         meta:set_int("src_time", meta:get_int("src_time") + 1)
         if meta:get_int("src_time") >= result.time / data.speed then
            meta:set_int("src_time", 0)
            local output = result.output
            if type(output) ~= "table" then output = { output } end
            local output_stacks = {}
            for _, o in ipairs(output) do
               table.insert(output_stacks, ItemStack(o))
         if meta:get_int("src_time") < round(result.time*10) then
            if not powered then
               technic.swap_node(pos, machine_node)
               meta:set_string("infotext", S("%s Unpowered"):format(machine_desc_tier))
            end
            local room_for_output = true
            inv:set_size("dst_tmp", inv:get_size("dst"))
            inv:set_list("dst_tmp", inv:get_list("dst"))
            for _, o in ipairs(output_stacks) do
               if not inv:room_for_item("dst_tmp", o) then
                  room_for_output = false
                  break
               end
               inv:add_item("dst_tmp", o)
            end
            if room_for_output then
               inv:set_list("src", result.new_input)
               inv:set_list("dst", inv:get_list("dst_tmp"))
            else
            end
            return
         end
         local output = result.output
         if type(output) ~= "table" then output = { output } end
         local output_stacks = {}
         for _, o in ipairs(output) do
            table.insert(output_stacks, ItemStack(o))
         end
         local room_for_output = true
         inv:set_size("dst_tmp", inv:get_size("dst"))
         inv:set_list("dst_tmp", inv:get_list("dst"))
         for _, o in ipairs(output_stacks) do
            if not inv:room_for_item("dst_tmp", o) then
               room_for_output = false
               break
            end
            inv:add_item("dst_tmp", o)
         end
         if not room_for_output then
            technic.swap_node(pos, machine_node)
            meta:set_string("infotext", S("%s Idle"):format(machine_desc_tier))
            meta:set_int(tier.."_EU_demand", 0)
            meta:set_int("src_time", round(result.time*10))
            return
         end
         meta:set_int("src_time", meta:get_int("src_time") - round(result.time*10))
         inv:set_list("src", result.new_input)
         inv:set_list("dst", inv:get_list("dst_tmp"))
      end
      meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1])
   end
   
   minetest.register_node("technic:"..ltier.."_"..machine_name, {
@@ -128,6 +145,7 @@
      paramtype2 = "facedir",
      groups = groups,
      tube = data.tube and tube or nil,
      connect_sides = data.connect_sides or connect_default,
      legacy_facedir_simple = true,
      sounds = default.node_sound_wood_defaults(),
      on_construct = function(pos)
@@ -147,6 +165,8 @@
      allow_metadata_inventory_take = technic.machine_inventory_take,
      allow_metadata_inventory_move = technic.machine_inventory_move,
      technic_run = run,
      after_place_node = data.tube and pipeworks.after_place,
      after_dig_node = technic.machine_after_dig_node
   })
   minetest.register_node("technic:"..ltier.."_"..machine_name.."_active",{
@@ -160,6 +180,7 @@
      paramtype2 = "facedir",
      drop = "technic:"..ltier.."_"..machine_name,
      groups = active_groups,
      connect_sides = data.connect_sides or connect_default,
      legacy_facedir_simple = true,
      sounds = default.node_sound_wood_defaults(),
      tube = data.tube and tube or nil,