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

[TUT] Quarry

Started by baeshra, 27 February 2013 - 03:50 PM
baeshra #1
Posted 27 February 2013 - 04:50 PM
A simple quarry program.

[media]http://www.youtube.com/watch?v=8BmEMSAKuCI[/media]



args = {...}
if #args ~= 3 then
return
end
x,y,z = args[1], args[2], args[3]
xt = 0
yt = 1
zt = 0
mt = 0
rotate = 0
x = tonumber(x)
y = tonumber(y)
z = tonumber(z)

while true do

if zt == z then
return
end

while xt < x do
if turtle.forward() == true then
xt = xt + 1
else
 if zt == 0 then
  while turtle.detect() == true do
   turtle.dig()
   sleep(1)
  end
 else
   turtle.dig()
 end
end
end
mt = mt + 1

if xt == x and rotate == 0 and yt < y then
 turtle.turnRight()
 if zt == 0 then
  while turtle.detect() == true do
   turtle.dig()
   sleep(1)
  end
 else
  turtle.dig()
 end
 turtle.forward()
 turtle.turnRight()
 rotate = 1
 yt = yt + 1
 xt = 1
end

if xt == x and rotate == 1 and yt < y then
turtle.turnLeft()
if zt == 1 then
while turtle.detect() == true do
turtle.dig()
sleep(1)
end
else
turtle.dig()
end
turtle.forward()
turtle.turnLeft()
rotate = 0
xt = 1
yt = yt + 1
end

if mt == y and rotate == 0 and zt < z then
if zt ~= z then
turtle.digDown()
turtle.down()
else
return
end
turtle.turnLeft()
turtle.turnLeft()
yt = 1
mt = 0
zt = zt + 1
xt = 1
rotate = 0
end

if mt == y and rotate == 1 and zt < z then
reset = 1
while reset < x do
turtle.back()
reset = reset + 1
end
if zt ~= z then
turtle.digDown()
turtle.down()
else
return
end
yt = 1
mt = 0
zt = zt + 1
xt = 1
rotate = 0
end


shell.run('clear')
print(rotate)
print(mt)

os.queueEvent("Crash")
os.pullEvent()
end
shiphorns #2
Posted 28 February 2013 - 12:19 PM
Very interesting to see you work through the whole process. I highly recommend that you continue to develop and improve this, and periodically update with your findings. It's a long road between this type of exercise and a robust quarrying program, but when you start using the program for real work, you will discover how it needs to evolve, because you'll have a nice feedback loop (e.g. you'll find it doing bad things and have to code for them :-).

I would recommend progressing in the following order (approximately the order I added to my own quarry program):

1. Handle player and mob interactions. It's great that you are handling the return value of turtle.forward() the first place you use it in the code, but you need to do that every single time you use it, or you will find that each time your turtle encounters a mob, your quarry footprint will get a little smaller or it will offset from where it's supposed to be (because your code will increment the movement counters like xt and yt even if turtle.forward() fails). You should also attack mobs, or they can block the progress of the turtle indefinitely; imagine a zombie in a deep 1-block sized hole in front of your turtle–you can't dig him, turtle.forward() will only succeed if you kill him.

2. Add basic fuel and fullness checking, which just suspends execution if it runs out of fuel or fills up its inventory. Give the player a chance to refuel and empty the turtle, and hit a key to resume the program without losing its progress. Later on, you can improve this to refuel from inventory or make return trips to refuel from/dump to chest(s).

3. Optimize away your 'reset' trip after each layer. There is no need to start back at the same x,z position for each layer, just drop the turtle down where it is, and start the new layer from there. This means adding one more bit of direction logic, because if the layer has an even number of rows, the end-of-row turns on the next layer (and on every even numbered layer) will be the reverse of those used for the first layer.

4. Make the turtle return to where it started when it's done (or if it has to abort). This is important in a quarry program, because if your turtle goes under lava, you won't be able to retrieve it if it stops there.

5. Detect for bedrock, or unbreakable blocks (with mod packs, there are unbreakable types other than bedrock, such as blocks that need special user permissions to break), gracefully end the program if the turtle is blocked.

6. Make the turtle do return trips to a supply/refuel/dump chest. Alternately, have the turtle carry an ender chest it can dump into.

7. Improve fuel economy by taking advantage of the turtle's ability to dig above and below itself. The turtle really only needs to traverse every 3rd layer in order to clear a given volume. More falling sand/gravel code is needed if you use digUp(), as well as a check for bedrock above the turtle so that it doesn't get stuck by mining a path under bedrock.

P.S. your turtle digs down block at the end of your quarry because you always dig down at the end of a layer, and don't check for completion (zt==z) until the start of the next iteration.