Mining turtle - code problem

  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

Xhapan

New Member
Jul 29, 2019
3
0
0
Hi guys/girls,

I play FTB Infinity and currently having a problem with my mining turtle code.
I hope someone can help me.

function DigForward(x)
--Grabe eine gerade Linie
--x lang
local BlockDetected, Block = turtle.inspect()
local CriticalBlockDetected = false;
for i=1, x do

if BlockDetected then
while Block.name == "minecraft:gravel" or Block.name == "minecraft:sand" do
turtle.dig()
sleep(0.9)
CriticalBlockDetected = true
BlockDetected, Block = turtle.inspect()
end
end

if CriticalBlockDetected ~= true then
turtle.dig()
end

turtle.forward()
turtle.digUp()
CriticalBlockDetected = false

end

end

What it should do: Dig a straight line x long. If it detects gravel or sand: Dig, wait for the blocks to fall down and go forward if none come down anymore.

What it actually does: Digs a line x long if it is not gravel or sand. If it is gravel or sand, it digs 5 times and stops.

I don't have a clue why the program does that, because code wise it shouldn't do that. It feels a bit like the for loop counts while the while loop does its thing.

(Why do i write it this complicated ? Because i want to speed up the digging and only wait 0.9 seconds if there is actual gravel or sand. Otherwise dig or just go forward if there is no block at all. )

Br Xhapan
 

Henry Link

Forum Addict
Dec 23, 2012
2,601
553
153
USA - East Coast
The big issue here is you are digging and moving before detecting the block in front of the turtle. I usually prefer to just try and move the turtle. If it can't move then do another dig command. Just keep track of your successful move.

EDIT: After reading your code some more. You are only doing another block detect inside of the gravel/sand while loop. You need to have a turtle.inspect command right after you move forward.
 
Last edited:

Xhapan

New Member
Jul 29, 2019
3
0
0
The big issue here is you are digging and moving before detecting the block in front of the turtle. I usually prefer to just try and move the turtle. If it can't move then do another dig command. Just keep track of your successful move.

EDIT: After reading your code some more. You are only doing another block detect inside of the gravel/sand while loop. You need to have a turtle.inspect command right after you move forward.
Hi Henry,
thank you for the fast reply.

After a good night sleep i rewrote the code completly and found maybe my problem(s):

function DigForward(x)
--Grabe eine gerade Linie
--x lang
local BlockDetected, Block = 0
local steps = 0
while(steps < x) do
BlockDetected, Block = turtle.inspect()

if turtle.detect() == true then
while Block.name == "minecraft:gravel" or Block.name == "minecraft:sand" do
turtle.dig()
sleep(0.9)
BlockDetected, Block = turtle.inspect()
end

if Block.name ~= "minecraft:gravel" and Block.name ~= "minecraft:sand" then
turtle.dig()
end
else
turtle.forward()
if turtle.detectUp() == true then
turtle.digUp()
end
steps = steps + 1
end
end

end

Both codes had a problem, when there was gravel/sand on top of a cobble stone. then it would not go forward the expected amount that you gave it via x. This method is a bit slower than just going forward, but at least there is no gravel sand problem anymore and the waiting time was drastically reduced.

Is there a way to detect, if there is a gravel on top of the cobblestone in front of the turtle? Because then it would be the same speed.

Br Xhapan
 

Henry Link

Forum Addict
Dec 23, 2012
2,601
553
153
USA - East Coast
As far as I know there is not a way to detect that. The turtle works with blocked directly in front of it, below it or above it. It can't work with blocks on the diagonal that I am aware of.
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
You can't check diagonal. You can check if there is a block in front of the turtle and make a check function like
Code:
function checkBreak()
--makes it loop for as long there is a block in front of the turtle
while turtle.detect() do
   --mine the block
   turtle.dig()
   --prevent crash from looping too often without a sleep (20 game ticks = 1 second thus 1 tick=0,05 seconds)
   turtle.sleep(0.05)
end
end
use this before moving and it will wait and dig until all the gravel/sand/other blocks are gone before moving. Mobs still block it however.