Vanessa Ezekowitz
2014-07-23 29c7ff5228864bcf5456f391b122c9eb477c754b
technic/machines/HV/quarry.lua
@@ -3,41 +3,37 @@
minetest.register_craft({
   recipe = {
      {"technic:carbon_steel_block", "pipeworks:filter",           "technic:carbon_steel_block"},
      {"technic:carbon_steel_block", "technic:motor",              "technic:carbon_steel_block"},
      {"technic:carbon_steel_block", "technic:diamond_drill_head", "technic:carbon_steel_block"}},
      {"technic:carbon_plate",       "pipeworks:filter",       "technic:composite_plate"},
      {"technic:motor",              "technic:machine_casing", "technic:diamond_drill_head"},
      {"technic:carbon_steel_block", "technic:hv_cable0",      "technic:carbon_steel_block"}},
   output = "technic:quarry",
})
local quarry_dig_above_nodes = 3 -- How far above the quarry we will dig nodes
local quarry_max_depth       = 100
local function get_quarry_formspec(size)
   return "size[3,1.5]"..
      "field[1,0.5;2,1;size;Radius;"..size.."]"..
      "button[0,1;3,1;toggle;"..S("Enable/Disable").."]"
local function set_quarry_formspec(meta)
   local formspec = "size[3,1.5]"..
      "field[1,0.5;2,1;size;Radius;"..meta:get_int("size").."]"
   if meta:get_int("enabled") == 0 then
      formspec = formspec.."button[0,1;3,1;enable;"..S("%s Disabled"):format(S("%s Quarry"):format("HV")).."]"
   else
      formspec = formspec.."button[0,1;3,1;disable;"..S("%s Enabled"):format(S("%s Quarry"):format("HV")).."]"
   end
   meta:set_string("formspec", formspec)
end
local function quarry_receive_fields(pos, formname, fields, sender)
   local meta = minetest.get_meta(pos)
   local size = tonumber(fields.size) or 0
   if fields.toggle then
      if meta:get_int("enabled") == 0 then
         meta:set_int("enabled", 1)
      else
         meta:set_int("enabled", 0)
      end
   end
   -- Smallest size is 2. Largest is 8.
   size = math.max(size, 2)
   size = math.min(size, 8)
   if meta:get_int("size") ~= size then
   if fields.size then
      local size = tonumber(fields.size) or 0
      size = math.max(size, 2)
      size = math.min(size, 8)
      meta:set_int("size", size)
      meta:set_string("formspec", get_quarry_formspec(size))
   end
   if fields.enable then meta:set_int("enabled", 1) end
   if fields.disable then meta:set_int("enabled", 0) end
   set_quarry_formspec(meta)
end
local function get_quarry_center(pos, size)
@@ -103,19 +99,15 @@
      end
      if minetest.is_protected and minetest.is_protected(digpos, owner) then
         meta:set_int("enabled", 0)
         set_quarry_formspec(meta)
         return {}
      end
      dig_y = digpos.y
      local node = minetest.get_node(digpos)
      drops = minetest.get_node_drops(node.name, "")
      minetest.dig_node(digpos)
      if minetest.get_node(digpos).name == node.name then
         -- We tried to dig something undigable like a
         -- filled chest. Notice that we check for a node
         -- change, not for air. This is so that we get drops
         -- from things like concrete posts with platforms,
         -- which turn into regular concrete posts when dug.
         drops = {}
      local node_def = minetest.registered_nodes[node.name] or { diggable = false }
      if node_def.diggable and ((not node_def.can_dig) or node_def.can_dig(digpos, nil)) then
         minetest.remove_node(digpos)
         drops = minetest.get_node_drops(node.name, "")
      end
   elseif not (dig_y < pos.y - quarry_max_depth) then
      dig_y = dig_y - 16
@@ -134,22 +126,51 @@
   end
end
local run = function(pos, node)
   local meta = minetest.get_meta(pos)
   local size = meta:get_int("size")
   local eu_input = meta:get_int("HV_EU_input")
   local demand = 10000
   local center = get_quarry_center(pos, size)
   local dig_y = meta:get_int("dig_y")
   local machine_name = S("%s Quarry"):format("HV")
   if meta:get_int("enabled") == 0 then
      meta:set_string("infotext", S("%s Disabled"):format(machine_name))
      meta:set_int("HV_EU_demand", 0)
      return
   end
   if eu_input < demand then
      meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
   elseif eu_input >= demand then
      meta:set_string("infotext", S("%s Active"):format(machine_name))
      local items = quarry_dig(pos, center, size)
      send_items(items, pos, node)
      if dig_y < pos.y - quarry_max_depth then
         meta:set_string("infotext", S("%s Finished"):format(machine_name))
      end
   end
   meta:set_int("HV_EU_demand", demand)
end
minetest.register_node("technic:quarry", {
   description = S("Quarry"),
   description = S("%s Quarry"):format("HV"),
   tiles = {"technic_carbon_steel_block.png", "technic_carbon_steel_block.png",
            "technic_carbon_steel_block.png", "technic_carbon_steel_block.png",
            "technic_carbon_steel_block.png^default_tool_mesepick.png", "technic_carbon_steel_block.png"},
   paramtype2 = "facedir",
   groups = {cracky=2, tubedevice=1},
   groups = {cracky=2, tubedevice=1, technic_machine = 1},
   tube = {
      connect_sides = {top = 1},
   },
   on_construct = function(pos)
      local size = 4
      local meta = minetest.get_meta(pos)
      meta:set_string("infotext", S("Quarry"))
      meta:set_string("formspec", get_quarry_formspec(4))
      meta:set_int("size", size)
      meta:set_string("infotext", S("%s Quarry"):format("HV"))
      meta:set_int("size", 4)
      set_quarry_formspec(meta)
      meta:set_int("dig_y", pos.y)
   end,
   after_place_node = function(pos, placer, itemstack)
@@ -159,43 +180,7 @@
   end,
   after_dig_node = pipeworks.scan_for_tube_objects,
   on_receive_fields = quarry_receive_fields,
})
minetest.register_abm({
   nodenames = {"technic:quarry"},
   interval = 1,
   chance = 1,
   action = function(pos, node, active_object_count, active_object_count_wider)
      local meta = minetest.get_meta(pos)
      local size = meta:get_int("size")
      local eu_input = meta:get_int("HV_EU_input")
      local demand = 10000
      local center = get_quarry_center(pos, size)
      local dig_y = meta:get_int("dig_y")
      local machine_name = S("Quarry")
      technic.switching_station_timeout_count(pos, "HV")
      if meta:get_int("enabled") == 0 then
         meta:set_string("infotext", S("%s Disabled"):format(machine_name))
         meta:set_int("HV_EU_demand", 0)
         return
      end
      if eu_input < demand then
         meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
      elseif eu_input >= demand then
         meta:set_string("infotext", S("%s Active"):format(machine_name))
         local items = quarry_dig(pos, center, size)
         send_items(items, pos, node)
         if dig_y < pos.y - quarry_max_depth then
            meta:set_string("infotext", S("%s Finished"):format(machine_name))
         end
      end
      meta:set_int("HV_EU_demand", demand)
   end
   technic_run = run,
})
technic.register_machine("HV", "technic:quarry", technic.receiver)