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

turtle.dig weirdness

Started by Axim, 21 November 2014 - 02:19 PM
Axim #1
Posted 21 November 2014 - 03:19 PM
Hey guys

I'm currently in the process of writing my own mining scripts, and I've just run into weirdness.
I've noticed that most people will write their gravel handling like this

while turtle.detect() do turtle.dig() end
I kinda feel as though this wastes valuable computation cycles so I prefer

while turtle.dig() do end
which works perfectly fine clearing a gravel tower in front of the turtle. However

while turtle.digUp() do end
does not work to clear one on top. It'll just dig one block then fail to detect another and end. So a bit of trial and error later I find the exact amount of sleep I need to add to make it work, which is 0.35 seconds. However now it is considerably slower than the one digging in front. I tested this with two turtles both digging a 100 block high gravel tower; the turtle digging up had about 10 blocks left when the other was done.
If someone with a deep knowledge of ComputerCraft could explain to me why this happens, that would be awesome. I can live with doing things inefficiently, but only if I know I have to and why.
Or maybe if there's a workaround to make them both go full speed, that would be even better.

Thanks in advance,
Axim
KingofGamesYami #2
Posted 21 November 2014 - 04:30 PM
I normally write my scripts to attempt to move, and if not dig and attack. This allows the script to deal with mobs & players in addition to gravel and sand.

local function forward()
  while not turtle.forward() do
   turtle.dig()
   turtle.attack()
  end
end
Axim #3
Posted 22 November 2014 - 03:04 AM
Not quite what I asked for, but thanks for your input. I don't think your way would work for me since one of my main goals is fuel efficiency. For one thing I want my quarry turtle to dig 3 layers for every movement.
civilwargeeky #4
Posted 22 November 2014 - 04:01 AM
So, if your are looking for time efficiency, I have a bit of input.

If you want to detect gravel falling on top of the turtle, you will have to waste a little bit of time no matter what you do. This is because the turtle.dig function takes something like 0.2 seconds (someone did the research on this, I don't remember where though). So what happens is you go forward, you mine up, releasing a gravel block. While the block is falling, there is nothing else there, so the turtle moves to the next step. You mine down and move on.

If you wanted to be most time efficient, you would mine up, mine down, then check again. However, the gravel still has 0.15 seconds (according to your research) before it finishes falling. You will have to sleep for this time.

Assuming you have done the above, you now have two options, you could try detecting (I think 0.05 seconds), then dig if true, or just try digging (0.2 seconds). If either returned true, you would wait 0.35 seconds again.

No matter which option you pick, every dig move you make will be at least 0.2 seconds slower than a regular dig move if you want to be certain that you got gravel.
The code for this would look like this:

while not turtle.forward() do
  while turtle.dig() do end
  local flag = turtle.digUp()
  turtle.digDown()
  if flag then
	--#sleep(0.15) --#Wait for gravel. This should be unnecessary, see edit
	if turtle.detectUp() then
	  while turtle.digUp() do
		sleep(0.35)
	  end
	end
  end
end

Another thing to note (that I recently modified in my quarry) is that if you plan for more than half of your path to be mining, then put another "turtle.dig()" before your "while not turtle.forward()" because a failed turtle.forward call wastes time.

I'll see if I can find that research page again, if you have any other questions or criticisms let me know :)/>

Edit: I found the post. It was from AustinKK in his quarry here . However, that post is from December 2012, so I'm not sure how accurate it is.

Move (forward/back/up/down): 0.4s
Turn (right/left): 0.4s
Dig (up/down/front): 0.4s
Detect (front/up/down): 0.05s
Compare (front/up/down): 0.05s

He says that apparently a turtle digging costs 0.4 seconds, not 0.2 seconds. Therefore, you wouldn't have to sleep for 0.15 seconds, and your turtle would only be 0.05 seconds slower per movement. As long as your measurements are accurate, you should be good to go with just checking after you dig down. :)/>
Edited on 22 November 2014 - 03:11 AM