Zefram
2014-07-15 dd65a68ce9f494717faffc98c45814f9a9d67fa4
technic/machines/register/machine_base.lua
@@ -17,14 +17,14 @@
function technic.register_base_machine(data)
   local typename = data.typename
   local numitems = technic.recipes[typename].numitems
   local input_size = technic.recipes[typename].input_size
   local machine_name = data.machine_name
   local machine_desc = data.machine_desc
   local tier = data.tier
   local ltier = string.lower(tier)
   local groups = {cracky = 2}
   local active_groups = {cracky = 2, not_in_creative_inventory = 1}
   local groups = {cracky = 2, technic_machine = 1}
   local active_groups = {cracky = 2, technic_machine = 1, not_in_creative_inventory = 1}
   if data.tube then
      groups.tubedevice = 1
      groups.tubedevice_receiver = 1
@@ -35,7 +35,7 @@
   local formspec =
      "invsize[8,9;]"..
      "list[current_name;src;"..(4-numitems)..",1;"..numitems..",1;]"..
      "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).."]"
@@ -46,6 +46,77 @@
         "label[1,4;"..S("Upgrade Slots").."]"
   end
   local run = function(pos, node)
      local meta     = minetest.get_meta(pos)
      local inv      = meta:get_inventory()
      local eu_input = meta:get_int(tier.."_EU_input")
      local machine_desc_tier = machine_desc:format(tier)
      local machine_node      = "technic:"..ltier.."_"..machine_name
      local machine_demand    = data.demand
      -- Setup meta data if it does not exist.
      if not eu_input then
         meta:set_int(tier.."_EU_demand", machine_demand[1])
         meta:set_int(tier.."_EU_input", 0)
         return
      end
      local EU_upgrade, tube_upgrade = 0, 0
      if data.upgrade then
         EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
      end
      if data.tube then
         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
      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
         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))
            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
         end
      end
      meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1])
   end
   minetest.register_node("technic:"..ltier.."_"..machine_name, {
      description = machine_desc:format(tier),
      tiles = {"technic_"..ltier.."_"..machine_name.."_top.png", 
@@ -66,7 +137,7 @@
         meta:set_int("tube_time",  0)
         meta:set_string("formspec", formspec)
         local inv = meta:get_inventory()
         inv:set_size("src", numitems)
         inv:set_size("src", input_size)
         inv:set_size("dst", 4)
         inv:set_size("upgrade1", 1)
         inv:set_size("upgrade2", 1)
@@ -75,6 +146,7 @@
      allow_metadata_inventory_put = technic.machine_inventory_put,
      allow_metadata_inventory_take = technic.machine_inventory_take,
      allow_metadata_inventory_move = technic.machine_inventory_move,
      technic_run = run,
   })
   minetest.register_node("technic:"..ltier.."_"..machine_name.."_active",{
@@ -95,70 +167,8 @@
      allow_metadata_inventory_put = technic.machine_inventory_put,
      allow_metadata_inventory_take = technic.machine_inventory_take,
      allow_metadata_inventory_move = technic.machine_inventory_move,
   })
   minetest.register_abm({
      nodenames = {"technic:"..ltier.."_"..machine_name,
                   "technic:"..ltier.."_"..machine_name.."_active"},
      interval = 1,
      chance   = 1,
      action = function(pos, node, active_object_count, active_object_count_wider)
         local meta     = minetest.get_meta(pos)
         local inv      = meta:get_inventory()
         local eu_input = meta:get_int(tier.."_EU_input")
         local machine_desc_tier = machine_desc:format(tier)
         local machine_node      = "technic:"..ltier.."_"..machine_name
         local machine_demand    = data.demand
         -- Setup meta data if it does not exist.
         if not eu_input then
            meta:set_int(tier.."_EU_demand", machine_demand[1])
            meta:set_int(tier.."_EU_input", 0)
            return
         end
         -- Power off automatically if no longer connected to a switching station
         technic.switching_station_timeout_count(pos, tier)
         local EU_upgrade, tube_upgrade = 0, 0
         if data.upgrade then
            EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
         end
         if data.tube then
            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
         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
            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 result_stack = ItemStack(result.output)
               if inv:room_for_item("dst", result_stack) then
                  inv:set_list("src", result.new_input)
                  inv:add_item("dst", result_stack)
               end
            end
         end
         meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1])
      end
      technic_run = run,
      technic_disabled_machine_name = "technic:"..ltier.."_"..machine_name,
   })
   technic.register_machine(tier, "technic:"..ltier.."_"..machine_name,            technic.receiver)