This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
wookiederk's profile picture

Turtle Digging Code

Started by wookiederk, 05 June 2013 - 11:46 AM
wookiederk #1
Posted 05 June 2013 - 01:46 PM
Hi, this is my attempt to make a customized turtle digging code. I'm stumped on how to fix its funky movements. Upon digging to the next level, it digs 1 block over the right amount, and upon its second level down it turns in the completely wrong direction. Here is my code:
 term.clear()
term.setCursorPos(1, 1)
function status(a, B)/> return print("Block Number X, Z, Y: "), print(k..", "..j..", "..i) end
while true do
 print("Length: ")
 input = read()
 if tonumber(input) then
  x = tonumber(input)
  break
 else
  print("Input must be a number!")
  end
 end
while true do
 print("Width: ")
 input = read()
 if tonumber(input) then
  z = tonumber(input)
  break
 else
  print("Input must be a number!")
  end
 end
while true do
 print("Depth: ")
 input = read()
 if tonumber(input) then
  y = tonumber(input)
  break
 else
  print("Input must be a number!")
  end
 end
i = 0
j = 0
k = 0
depth = y
length = x
width = z
right = true
direction = true
pattern = true
while i < y do
 while j < z do
  while k < x do
   turtle.dig()
   turtle.forward()
   status()
   k = k + 1
   end
  if right then
   turtle.turnRight()
   turtle.dig()
   turtle.forward()
   turtle.turnRight()
   right = false
  else
   turtle.turnLeft()
   turtle.dig()
   turtle.forward()
   turtle.turnLeft()
   right = true
   end
  k = 0
  j = j + 1
  end
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
  while k < x do
   turtle.dig()
   turtle.forward()
   k = k + 1
   end
  k = 0
  turtle.turnLeft() 
  turtle.digDown()
  turtle.down()
  turtle.dig()
  turtle.turnRight()
  turtle.turnRight()
  right = true
 j = 0
 if pattern then
  x = width
  z = length
  pattern = false
 else
  x = length
  z = width
  pattern = true
  end
 i = i + 1
 end
turtle.up()
print("Finished!")


Thanks for any help
Bomb Bloke #2
Posted 05 June 2013 - 08:10 PM
.
.
  k = 0
  j = j + 1
  end
  turtle.turnRight()
  turtle.forward()    -- Without a dig command about here, odds are the turtle won't go forward on many occasions.
  turtle.turnLeft()
  while k < x do
   turtle.dig()
   turtle.forward()
   k = k + 1
.
.

Also bear in mind that falling gravel/sand/etc may cause issues. You may wish to define your own function for moving forward and call that - have it check whether "turtle.forward()" actually returned true, and if it didn't try digging again etc until it does.

Oh, and try "for" loops. This code here:

  while k < x do
   turtle.dig()
   turtle.forward()
   k = k + 1
   end
  k = 0

Can be replaced with this code here:

  for k=0,x-1 do
   turtle.dig()
   turtle.forward()
   end

It does exactly the same thing, though it effectively wipes "k" entirely when complete instead of just setting it to 0.
wookiederk #3
Posted 06 June 2013 - 12:40 PM
Thanks that did help a little bit. But the rotation upon moving down to the next level is all wrong. Once the turtle moves down to the next level it spins in a funky way. Any help there?
Spoiler
term.clear()
term.setCursorPos(1, 1)
function status(a, B)/>/> return print("Block Number X, Z, Y: "), print(k..", "..j..", "..i) end
function moveForward()
while true do
if turtle.forward() then
  break
else
  turtle.dig()
  end
end
end
while true do
print("Length: ")
input = read()
if tonumber(input) then
  x = tonumber(input)
  break
else
  print("Input must be a number!")
  end
end
while true do
print("Width: ")
input = read()
if tonumber(input) then
  z = tonumber(input)
  break
else
  print("Input must be a number!")
  end
end
while true do
print("Depth: ")
input = read()
if tonumber(input) then
  y = tonumber(input)
  break
else
  print("Input must be a number!")
  end
end
i = 0
j = 0
k = 0
depth = y
length = x
width = z
right = true
direction = true
pattern = true
while i < y do
while j < z do
  while k < x do
   moveForward()
   status()
   k = k + 1
   end
  if right then
   turtle.turnRight()
   moveForward()
   turtle.turnRight()
   right = false
  else
   turtle.turnLeft()
   moveForward()
   turtle.turnLeft()
   right = true
   end
  k = 0
  j = j + 1
  end
  turtle.turnRight()
  moveForward()
  turtle.turnLeft()
  for k =0,x-1 do
   moveForward()
   end
  turtle.turnLeft()
  turtle.digDown()
  turtle.down()
  turtle.dig()
  turtle.turnRight()
  turtle.turnRight()
  right = true
j = 0
if pattern then
  x = width
  z = length
  pattern = false
else
  x = length
  z = width
  pattern = true
  end
i = i + 1
end
turtle.up()
print("Finished!")

Bomb Bloke #4
Posted 06 June 2013 - 10:38 PM
After completing each layer, the turtle will either be facing the direction in which it was facing when it started that layer (if the amount of lanes to dig were odd), or facing the opposite direction (if they were even). This means you need to account for the current value of "right" when going down a level.

Changing the end of your code to something like this should do the trick:

Spoiler
.
.
.
   right = true
   end
  k = 0
  j = j + 1
  end

  -- "j" while loop just ended here. Changes below.

  right = not right
  if right then turtle.turnRight() else turtle.turnLeft() end
  turtle.digDown()
  turtle.down()

j = 0
if pattern then
  x = width
  z = length
else
  x = length
  z = width
end

pattern = not pattern
i = i + 1
end
turtle.up()  -- Not going all the way up?
print("Finished!")

You may also find the turtle is digging its pit just a bit longer then you want it to in your current implementation. Changing your "while k < x do" line to "while k < x-1 do" should sort that.