Untitled

 avatar
unknown
plain_text
10 months ago
5.0 kB
16
Indexable
--[[
  Excavator Program for ComputerCraft (v14 - "Place Behind" Torches)
  Digs a room, places torches on the floor behind itself, returns, and deposits items.

  --------------------------
  --   USAGE INSTRUCTIONS   --
  --------------------------

  1. PLACEMENT:
     - Place the turtle at the desired BACK, BOTTOM, LEFT corner of the room.
     - The turtle must be facing the direction you want the LENGTH of the room to extend.

  2. CHEST & FUEL:
     - Place a single chest directly BEHIND the turtle. It will use this to dump items.
     - Place fuel (coal, charcoal, etc.) in the LAST inventory slot (Slot 16).
     - (Optional) Place torches in Slot 15. They will only be placed on the first layer.

  3. RUNNING THE PROGRAM:
     - Save this script on the turtle (e.g., as "excavate").
     - Run from the command line with three numbers for the dimensions.
     
     Syntax:  excavate <length> <width> <height>
     Example: excavate 30 10 5
]]

-- Configuration
local FUEL_SLOT = 16
local TORCH_SLOT = 15
local DUMP_START_SLOT = 1
local TORCH_SPACING = 12 -- Places a torch every 12 blocks along a row

-- ===== Argument Handling =====
local args = {...}
if #args ~= 3 then
  print("Usage: excavate <length> <width> <height>")
  return
end

local length = tonumber(args[1])
local width = tonumber(args[2])
local height = tonumber(args[3])

if not length or not width or not height or length < 1 or width < 1 or height < 1 then
  print("Error: Dimensions must be positive numbers.")
  return
end

print("Starting excavation of a " .. length .. "x" .. width .. "x" .. height .. " room.")
print("Setup confirmed. See comments in code for instructions.")
sleep(3)

-- ===== Helper Functions =====

local function tryRefuel()
  if turtle.getFuelLevel() < (length + width) then
    turtle.select(FUEL_SLOT)
    if turtle.refuel(1) then
      print("Refueled.")
    else
      print("Warning: Out of fuel in slot " .. FUEL_SLOT .. "!")
    end
    turtle.select(1)
  end
end

-- MODIFIED: Turns around and places the torch on the floor behind itself.
local function tryPlaceTorch()
    turtle.select(TORCH_SLOT)
    if turtle.getItemCount(TORCH_SLOT) > 0 then
      -- Turn around 180 degrees
      turtle.turnRight()
      turtle.turnRight()
      -- Place the torch on the block in front (which is the block behind its original orientation)
      if turtle.place() then
        print("Placed a torch.")
      end
      -- Turn back around 180 degrees
      turtle.turnRight()
      turtle.turnRight()
    end
    turtle.select(1) -- Always reselect slot 1
end

local function dumpInventory()
  turtle.turnRight()
  turtle.turnRight()
  for i = DUMP_START_SLOT, TORCH_SLOT - 1 do
    turtle.select(i)
    if turtle.getItemCount(i) > 0 then
      turtle.drop()
    end
  end
  turtle.select(1)
  turtle.turnRight()
  turtle.turnRight()
  print("Inventory emptied.")
end

local function checkInventory()
  local isFull = true
  for i = DUMP_START_SLOT, TORCH_SLOT - 1 do
    if turtle.getItemCount(i) == 0 then
      isFull = false
      break
    end
  end
  if isFull then
    print("Inventory full, dumping...")
    dumpInventory()
  end
end

local function safeForward()
  tryRefuel()
  checkInventory()
  while turtle.detect() do
    turtle.dig()
    sleep(0.1)
  end
  turtle.forward()
end

local function safeUp()
  tryRefuel()
  checkInventory()
  while turtle.detectUp() do
    if not turtle.digUp() then
      print("Cannot dig block above, stopping.")
      error()
    end
    sleep(0.1)
  end
  turtle.up()
end

local function returnToStart()
  if width % 2 ~= 0 then 
    turtle.turnRight(); turtle.turnRight()
    for i = 1, length - 1 do safeForward() end
    turtle.turnRight()
    for i = 1, width - 1 do safeForward() end
    turtle.turnRight()
  else
    turtle.turnRight()
    for i = 1, width - 1 do safeForward() end
    turtle.turnRight()
  end
end

-- ===== Main Mining Logic =====

for y = 1, height do
  for z = 1, width do
    -- Only place torches on the first layer
    if y == 1 and z > 1 then
        tryPlaceTorch()
    end

    for x = 1, length - 1 do
      -- Only place torches on the first layer
      if y == 1 and x % TORCH_SPACING == 0 then
        tryPlaceTorch()
      end
      turtle.digUp()
      safeForward()
    end
    
    if z < width then
      turtle.digUp()
      if z % 2 ~= 0 then
        turtle.turnRight()
        turtle.digUp()
        safeForward()
        turtle.turnRight()
      else
        turtle.turnLeft()
        turtle.digUp()
        safeForward()
        turtle.turnLeft()
      end
    end
  end
  
  if y < height then
    print("Layer " .. y .. " complete. Moving to next layer.")
    returnToStart()
    safeUp()
  end
end

-- ===== Final Return =====

print("Final cleanup: Returning to origin.")
returnToStart()

print("Moving back down to starting level...")
for i = 1, height - 1 do
  turtle.down()
end

print("Depositing final items...")
dumpInventory()

print("Excavation complete! All items deposited. ⛏️")
Editor is loading...
Leave a Comment