Vitaliy
2018-07-21 d1b54a573c6287ea1d63b9c3f979c39058b1331a
technic/machines/register/battery_box.lua
@@ -2,18 +2,69 @@
local digilines_path = minetest.get_modpath("digilines")
local S = technic.getter
local tube_entry = "^pipeworks_tube_connection_metallic.png"
local cable_entry = "^technic_cable_connection_overlay.png"
local fs_helpers = pipeworks.fs_helpers
technic.register_power_tool("technic:battery", 10000)
technic.register_power_tool("technic:red_energy_crystal", 50000)
technic.register_power_tool("technic:green_energy_crystal", 150000)
technic.register_power_tool("technic:blue_energy_crystal", 450000)
-- Battery recipes:
-- Tin-copper recipe:
minetest.register_craft({
   output = 'technic:battery',
   output = "technic:battery",
   recipe = {
      {'group:wood', 'default:copper_ingot', 'group:wood'},
      {'group:wood', 'moreores:tin_ingot',   'group:wood'},
      {'group:wood', 'default:copper_ingot', 'group:wood'},
      {"group:wood", "default:copper_ingot", "group:wood"},
      {"group:wood", "default:tin_ingot",    "group:wood"},
      {"group:wood", "default:copper_ingot", "group:wood"},
   }
})
-- Sulfur-lead-water recipes:
-- With sulfur lumps:
-- With water:
minetest.register_craft({
   output = "technic:battery",
   recipe = {
      {"group:wood",         "technic:sulfur_lump", "group:wood"},
      {"technic:lead_ingot", "bucket:bucket_water", "technic:lead_ingot"},
      {"group:wood",         "technic:sulfur_lump", "group:wood"},
   },
   replacements = {
      {"bucket:bucket_water", "bucket:bucket_empty"}
   }
})
-- With oil extract:
minetest.register_craft({
   output = "technic:battery",
   recipe = {
      {"group:wood",         "technic:sulfur_lump",   "group:wood"},
      {"technic:lead_ingot", "homedecor:oil_extract", "technic:lead_ingot"},
      {"group:wood",         "technic:sulfur_lump",   "group:wood"},
   }
})
-- With sulfur dust:
-- With water:
minetest.register_craft({
   output = "technic:battery",
   recipe = {
      {"group:wood",         "technic:sulfur_dust", "group:wood"},
      {"technic:lead_ingot", "bucket:bucket_water", "technic:lead_ingot"},
      {"group:wood",         "technic:sulfur_dust", "group:wood"},
   },
   replacements = {
      {"bucket:bucket_water", "bucket:bucket_empty"}
   }
})
-- With oil extract:
minetest.register_craft({
   output = "technic:battery",
   recipe = {
      {"group:wood",         "technic:sulfur_dust",   "group:wood"},
      {"technic:lead_ingot", "homedecor:oil_extract", "technic:lead_ingot"},
      {"group:wood",         "technic:sulfur_dust",   "group:wood"},
   }
})
@@ -31,34 +82,79 @@
   }
})
-- x+2 + (z+2)*2
local dirtab = {
   [4] = 2,
   [5] = 3,
   [7] = 1,
   [8] = 0
}
local tube = {
   insert_object = function(pos, node, stack, direction)
      if direction.y == 0 then
      print(minetest.pos_to_string(direction), dirtab[direction.x+2+(direction.z+2)*2], node.param2)
      if direction.y == 1
         or (direction.y == 0 and dirtab[direction.x+2+(direction.z+2)*2] == node.param2) then
         return stack
      end
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      if direction.y > 0 then
      if direction.y == 0 then
         return inv:add_item("src", stack)
      else
         return inv:add_item("dst", stack)
      end
   end,
   can_insert = function(pos, node, stack, direction)
      if direction.y == 0 then
      print(minetest.pos_to_string(direction), dirtab[direction.x+2+(direction.z+2)*2], node.param2)
      if direction.y == 1
         or (direction.y == 0 and dirtab[direction.x+2+(direction.z+2)*2] == node.param2) then
         return false
      end
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      local onestack = stack:peek_item(1)
      if direction.y > 0 then
         return inv:room_for_item("src", onestack)
      if direction.y == 0 then
         if meta:get_int("split_src_stacks") == 1 then
            stack = stack:peek_item(1)
         end
         return inv:room_for_item("src", stack)
      else
         return inv:room_for_item("dst", onestack)
         if meta:get_int("split_dst_stacks") == 1 then
            stack = stack:peek_item(1)
         end
         return inv:room_for_item("dst", stack)
      end
   end,
   connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
   connect_sides = {left=1, right=1, back=1, top=1},
}
local function add_on_off_buttons(meta, ltier, charge_percent)
   local formspec = ""
   if ltier == "mv" or ltier == "hv" then
      formspec = "image[1,1;1,2;technic_power_meter_bg.png"
         .."^[lowpart:"..charge_percent
         ..":technic_power_meter_fg.png]"..
         fs_helpers.cycling_button(
            meta,
            "image_button[3,2.0;1,0.6",
            "split_src_stacks",
            {
               pipeworks.button_off,
               pipeworks.button_on
            }
         ).."label[3.9,2.01;Allow splitting incoming 'charge' stacks from tubes]"..
         fs_helpers.cycling_button(
            meta,
            "image_button[3,2.5;1,0.6",
            "split_dst_stacks",
            {
               pipeworks.button_off,
               pipeworks.button_on
            }
         ).."label[3.9,2.51;Allow splitting incoming 'discharge' stacks]"
   end
   return formspec
end
function technic.register_battery_box(data)
   local tier = data.tier
@@ -79,6 +175,7 @@
      "listring[current_player;main]"..
      "listring[context;src]"..
      "listring[current_player;main]"
   if digilines_path then
      formspec = formspec.."button[0.6,3.7;2,1;edit_channel;edit Channel]"
   end
@@ -95,7 +192,14 @@
   end
   local run = function(pos, node)
      local below = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z})
      local meta           = minetest.get_meta(pos)
      if not technic.is_tier_cable(below.name, tier) then
         meta:set_string("infotext", S("%s Battery Box Has No Network"):format(tier))
         return
      end
      local eu_input       = meta:get_int(tier.."_EU_input")
      local current_charge = meta:get_int("internal_EU_charge")
@@ -150,14 +254,10 @@
      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("@1 Battery Box: @2/@3", tier,
            technic.pretty_num(current_charge), technic.pretty_num(max_charge))
      meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, charge_percent))
      local infotext = S("@1 Battery Box: @2 / @3", tier,
            technic.EU_string(current_charge),
            technic.EU_string(max_charge))
      if eu_input == 0 then
         infotext = S("%s Idle"):format(infotext)
      end
@@ -176,15 +276,26 @@
         groups.tubedevice_receiver = 1
      end
      local top_tex = "technic_"..ltier.."_battery_box_top.png"..tube_entry
      local front_tex = "technic_"..ltier.."_battery_box_front.png^technic_power_meter"..i..".png"
      local side_tex = "technic_"..ltier.."_battery_box_side.png"..tube_entry
      local bottom_tex = "technic_"..ltier.."_battery_box_bottom.png"..cable_entry
      if ltier == "lv" then
         top_tex = "technic_"..ltier.."_battery_box_top.png"
         front_tex = "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png"
         side_tex = "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png"
      end
      minetest.register_node("technic:"..ltier.."_battery_box"..i, {
         description = S("%s Battery Box"):format(tier),
         tiles = {
            "technic_"..ltier.."_battery_box_top.png",
            "technic_"..ltier.."_battery_box_bottom.png",
            "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png",
            "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png",
            "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png",
            "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png"},
            top_tex,
            bottom_tex,
            side_tex,
            side_tex,
            side_tex,
            front_tex},
         groups = groups,
         connect_sides = {"bottom"},
         tube = data.tube and tube or nil,
@@ -193,11 +304,17 @@
         drop = "technic:"..ltier.."_battery_box0",
         on_construct = function(pos)
            local meta = minetest.get_meta(pos)
            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)
            local charge = meta:get_int("internal_EU_charge")
            local cpercent = math.floor(charge / max_charge * 100)
            local inv = meta:get_inventory()
            local node = minetest.get_node(pos)
            meta:set_string("infotext", S("%s Battery Box"):format(tier))
            meta:set_string("formspec", formspec)
            meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, cpercent))
            meta:set_string("channel", ltier.."_battery_box"..minetest.pos_to_string(pos))
            meta:set_int(tier.."_EU_demand", 0)
            meta:set_int(tier.."_EU_supply", 0)
@@ -213,16 +330,32 @@
         allow_metadata_inventory_take = technic.machine_inventory_take,
         allow_metadata_inventory_move = technic.machine_inventory_move,
         technic_run = run,
         on_rotate = screwdriver.rotate_simple,
         after_place_node = data.tube and pipeworks.after_place,
         after_dig_node = technic.machine_after_dig_node,
         on_receive_fields = function(pos, formname, fields, sender)
            if not fields.edit_channel then
               return
            end
            local meta = minetest.get_meta(pos)
            minetest.show_formspec(sender:get_player_name(),
            local nodename = minetest.get_node(pos).name
            if fields.edit_channel then
               minetest.show_formspec(sender:get_player_name(),
                  "technic:battery_box_edit_channel"..minetest.pos_to_string(pos),
                  "field[channel;Digiline Channel;"..meta:get_string("channel").."]")
            elseif fields["fs_helpers_cycling:0:split_src_stacks"]
              or   fields["fs_helpers_cycling:0:split_dst_stacks"]
              or   fields["fs_helpers_cycling:1:split_src_stacks"]
              or   fields["fs_helpers_cycling:1:split_dst_stacks"] then
               local meta = minetest.get_meta(pos)
               if not pipeworks.may_configure(pos, sender) then return end
               fs_helpers.on_receive_fields(pos, fields)
               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)
               local charge = meta:get_int("internal_EU_charge")
               local cpercent = math.floor(charge / max_charge * 100)
               meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, cpercent))
            end
         end,
         digiline = {
            receptor = {action = function() end},
@@ -277,7 +410,6 @@
      meta:set_string("channel", fields.channel)
   end
)
function technic.charge_tools(meta, batt_charge, charge_step)
   local inv = meta:get_inventory()