CCCPaste

Turtle Cube v1.1

¨args={...} -- Basicaly write *yourprogram* CubeDepth CubeWidth CubeHeight - for example | mine 2 2 | mines out cube 2x2x1
x=tonumber(args[1])   -- Takes negative dimensions in all directions too!
if x == nil then
 x=1
end
y=tonumber(args[2])
if y == nil then
 y=1
end
z=tonumber(args[3])
if z == nil then
 z=1
end
moves=0
-- Few functions so that the code is "easier" to code  (this mainly includes weird exceptions and failsafes)
function midPrint(text,color)
 term.setBackgroundColor(color)
 term.clear()
 r,t=term.getSize()
 t=t/2+1
 r=(r/2)-(string.len(text)/2)+1
 term.setCursorPos(r,t)
 print(text)
end
function Forward()
 while not turtle.detect() and not turtle.forward() do
 midPrint("Blocked!",colors.red)
 end
end
function Dig()
 while turtle.detect() do
  turtle.dig()
 end
end
function Up()
 while not turtle.detectUp() and not turtle.up() do
  midPrint("Blocked!",colors.red)
 end
end
function DigUp()
 while turtle.detectUp() do
  turtle.digUp()
 end
end
function Down()
 while not turtle.detectDown() and not turtle.down() do
  midPrint("Blocked!",colors.red)
 end
end
function DigDown()
 while turtle.detectDown() do
  turtle.digDown()
 end
end

local yOdd=false
-- Negative coords logic conversion
if z<0 then
 Zneg=true
 z=z*(-1)
end
if y<0 and x>0 then
 turtle.turnLeft()
 Ymem=y
 y=x
 x=Ymem*(-1)
elseif y<0 and x<0 then
 turtle.turnLeft(2)
 yOdd=true
 Ymem=y
 y=x*(-1)
 x=Ymem*(-1)
elseif y>0 and x<0 then
 turtle.turnRight()
 Ymem=y
 y=x*(-1)
 x=Ymem
end
-- Main loop
if turtle.getFuelLevel()<(x*y*z) then
    local z=16
    x=turtle.getItemDetail()
    while turtle.select(z) do
       if turtle.getItemDetail() then
           if (tostring(turtle.getItemDetail().name)=="minecraft:coal") then
               break
           end
        end
        z=z-1
        if z==0 then
            z=16
        end
    end
    while (turtle.getItemCount())<(((x*y*z)//100)+1) do
        midPrint("Not enough Fuel!",colors.green)
    end
    turtle.refuel(((x*y*z)//100)+1)
end
local l=false
for i=z-1,0,-1 do
 for m=y-1,1,-1 do
  for n=x-1,1,-1 do
   Dig()
   Forward()
   moves=moves+1
   midPrint((tostring((moves/(x*y*z))*100).."%"),colors.green)
  end
  if not yOdd then
   if not l then
    yOdd=true
    turtle.turnRight()
   end
   if x==1 and not l then
    yOdd=false
    l=true
   end
   Dig()
   Forward()
   moves=moves+1
   midPrint((tostring((moves/(x*y*z))*100).."%"),colors.green)
   if not l then
    turtle.turnRight()
   end
  else
   yOdd=false
   turtle.turnLeft()
   Dig()
   Forward()
   moves=moves+1
   midPrint((tostring((moves/(x*y*z))*100).."%"),colors.green)
   turtle.turnLeft()
  end
 end
 for n=x-1,1,-1 do
  Dig()
  Forward()
  moves=moves+1
  midPrint((tostring((moves/(x*y*z))*100).."%"),colors.green)
 end
 if i>0 then
  if Zneg then
   DigDown()
   Down()
   moves=moves+1
   midPrint((tostring((moves/(x*y*z))*100).."%"),colors.green)
   if x>1 or y>1 then
    turtle.turnLeft()
    turtle.turnLeft()
   end
  else
   DigUp()
   Up()
   moves=moves+1
   midPrint((tostring((moves/(x*y*z))*100).."%"),colors.green)
   if x>1 or y>1 then
    turtle.turnLeft()
    turtle.turnLeft()
   end
  end
 end
end
midPrint("Complete",colors.blue)
term.setCursorPos(1,1)