Zefram
2014-08-18 7d610b7c80487cd7a6e66f55c9c3b1190e5dfc7f
technic/machines/register/battery_box.lua
@@ -80,8 +80,78 @@
         "label[3.5,4;"..S("Upgrade Slots").."]"
   end
   local run = function(pos, node)
      local meta           = minetest.get_meta(pos)
      local eu_input       = meta:get_int(tier.."_EU_input")
      local current_charge = meta:get_int("internal_EU_charge")
      local EU_upgrade, tube_upgrade = 0, 0
      if data.upgrade then
         EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
      end
      local max_charge = data.max_charge * (1 + EU_upgrade / 10)
      -- Charge/discharge the battery with the input EUs
      if eu_input >= 0 then
         current_charge = math.min(current_charge + eu_input, max_charge)
      else
         current_charge = math.max(current_charge + eu_input, 0)
      end
      -- Charging/discharging tools here
      local tool_full, tool_empty
      current_charge, tool_full = technic.charge_tools(meta,
            current_charge, data.charge_step)
      current_charge, tool_empty = technic.discharge_tools(meta,
            current_charge, data.discharge_step,
            max_charge)
      if data.tube then
         local inv = meta:get_inventory()
         technic.handle_machine_pipeworks(pos, tube_upgrade,
         function(pos, x_velocity, z_velocity)
            if tool_full and not inv:is_empty("src") then
               technic.send_items(pos, x_velocity, z_velocity, "src")
            elseif tool_empty and not inv:is_empty("dst") then
               technic.send_items(pos, x_velocity, z_velocity, "dst")
            end
         end)
      end
      -- We allow batteries to charge on less than the demand
      meta:set_int(tier.."_EU_demand",
            math.min(data.charge_rate, max_charge - current_charge))
      meta:set_int(tier.."_EU_supply",
            math.min(data.discharge_rate, current_charge))
         meta:set_int("internal_EU_charge", current_charge)
      -- Select node textures
      local charge_count = math.ceil((current_charge / max_charge) * 8)
      charge_count = math.min(charge_count, 8)
      charge_count = math.max(charge_count, 0)
      local last_count = meta:get_float("last_side_shown")
      if charge_count ~= last_count then
         technic.swap_node(pos,"technic:"..ltier.."_battery_box"..charge_count)
         meta:set_float("last_side_shown", charge_count)
      end
      local charge_percent = math.floor(current_charge / max_charge * 100)
      meta:set_string("formspec",
         formspec..
         "image[1,1;1,2;technic_power_meter_bg.png"
         .."^[lowpart:"..charge_percent
         ..":technic_power_meter_fg.png]")
      local infotext = S("%s Battery Box: %d/%d"):format(tier,
            current_charge, max_charge)
      if eu_input == 0 then
         infotext = S("%s Idle"):format(infotext)
      end
      meta:set_string("infotext", infotext)
   end
   for i = 0, 8 do
      local groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2}
      local groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_machine=1}
      if i ~= 0 then
         groups.not_in_creative_inventory = 1
      end
@@ -124,92 +194,9 @@
         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,
      })
   end
   minetest.register_abm({
      nodenames = {"technic:"..ltier.."_battery_box0", "technic:"..ltier.."_battery_box1",
                   "technic:"..ltier.."_battery_box2", "technic:"..ltier.."_battery_box3",
                   "technic:"..ltier.."_battery_box4", "technic:"..ltier.."_battery_box5",
                   "technic:"..ltier.."_battery_box6", "technic:"..ltier.."_battery_box7",
                   "technic:"..ltier.."_battery_box8"},
      interval = 1,
      chance   = 1,
      action = function(pos, node, active_object_count, active_object_count_wider)
         local meta           = minetest.get_meta(pos)
         local eu_input       = meta:get_int(tier.."_EU_input")
         local current_charge = meta:get_int("internal_EU_charge")
         -- 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
         local max_charge = data.max_charge * (1 + EU_upgrade / 10)
         -- Charge/discharge the battery with the input EUs
         if eu_input >= 0 then
            current_charge = math.min(current_charge + eu_input, max_charge)
         else
            current_charge = math.max(current_charge + eu_input, 0)
         end
         -- Charging/discharging tools here
         local tool_full, tool_empty
         current_charge, tool_full = technic.charge_tools(meta,
               current_charge, data.charge_step)
         current_charge, tool_empty = technic.discharge_tools(meta,
               current_charge, data.discharge_step,
               max_charge)
         if data.tube then
            local inv = meta:get_inventory()
            technic.handle_machine_pipeworks(pos, tube_upgrade,
            function(pos, x_velocity, z_velocity)
               if tool_full and not inv:is_empty("src") then
                  technic.send_items(pos, x_velocity, z_velocity, "src")
               elseif tool_empty and not inv:is_empty("dst") then
                  technic.send_items(pos, x_velocity, z_velocity, "dst")
               end
            end)
         end
         -- We allow batteries to charge on less than the demand
         meta:set_int(tier.."_EU_demand",
               math.min(data.charge_rate, max_charge - current_charge))
         meta:set_int(tier.."_EU_supply",
               math.min(data.discharge_rate, current_charge))
         meta:set_int("internal_EU_charge", current_charge)
         -- Select node textures
         local charge_count = math.ceil((current_charge / max_charge) * 8)
         charge_count = math.min(charge_count, 8)
         charge_count = math.max(charge_count, 0)
         local last_count = meta:get_float("last_side_shown")
         if charge_count ~= last_count then
            technic.swap_node(pos,"technic:"..ltier.."_battery_box"..charge_count)
            meta:set_float("last_side_shown", charge_count)
         end
         local charge_percent = math.floor(current_charge / max_charge * 100)
         meta:set_string("formspec",
            formspec..
            "image[1,1;1,2;technic_power_meter_bg.png"
            .."^[lowpart:"..charge_percent
            ..":technic_power_meter_fg.png]")
         local infotext = S("%s Battery Box: %d/%d"):format(tier,
               current_charge, max_charge)
         if eu_input == 0 then
            infotext = S("%s Idle"):format(infotext)
         end
         meta:set_string("infotext", infotext)
      end
   })
   -- Register as a battery type
   -- Battery type machines function as power reservoirs and can both receive and give back power