Main Menu

Mining Turtle Issues

Started by JarFlow37, Apr 15, 2022, 03:27 AM

Previous topic - Next topic

JarFlow37

Hi, I'm new to lua and computercraft.
I just picked up both about 3 days ago and have been making some basic programs, but I've hit a stop with this program. If someone could help me figure out what I'm doing wrong, I would really appreciate it.
     
--Mine a two block high tunnle as far the imputed distance
X = 0
Y = 0
print('How far should I mine?')
Block_imput = io.read()
io.write('Making ' .. Block_imput .. ' block long tunnel...')
 
Y = tonumber(Block_imput)
 
while X ~= Y do
     if turtle.detectUp() == true then
        turtle.digUp()
    elseif turtle.detect() == true then
        turtle.dig()
    end
 turtle.forward()
 X = X+1
 print(X)
end
 
 
print('Tunnel has been made.')

Erb3

Hello, And welcome to the world of Computercraft! 👋

I copied your code into a turtle in my creative world, and it worked just fine (Click for image). Therefore I suspect that the issue has to do with fuelling.
Fuelling in computercraft works by giving a turtle a burneable item, for example coal, and then running:
refuel [amount of items to refuel / all]Alternatively if you want to do it from code you can run:
turtle.refuel([Amount of item to refuel OPTIONAL ARGUMENT])Click for image.
Also remember that when refueling the turtle, it must have selected the slot with the fuel in. (Normally slot 1)
You can read more about fuelling here.


Even tho the code worked, i have made my own version of it.
print('How far should I mine?')
local input = tonumber(read())
print('Making ' .. input .. ' block long tunnel...')

for x=1,input do
  if turtle.detectUp() then
    turtle.digUp()
  elseif turtle.detect() then
    turtle.dig()
  end
 turtle.forward()
 print(x)
end

print("Tunnel has been made.")

I have changed the following:
  • io.read() and io.write() into print() and read()
  • Made a for loop instead of a while loop
  • Some other small changes


I hope you get a great time with ComputerCraft! 😀
It's not a data breach, it's a surprise backup.

JarFlow37

Quote from: PC_Cat on Apr 15, 2022, 08:08 AMHello, And welcome to the world of Computercraft! 👋

I copied your code into a turtle in my creative world, and it worked just fine (Click for image). Therefore I suspect that the issue has to do with fuelling.
Fuelling in computercraft works by giving a turtle a burneable item, for example coal, and then running:
refuel [amount of items to refuel / all]Alternatively if you want to do it from code you can run:
turtle.refuel([Amount of item to refuel OPTIONAL ARGUMENT])Click for image.
Also remember that when refueling the turtle, it must have selected the slot with the fuel in. (Normally slot 1)
You can read more about fuelling here.


Even tho the code worked, i have made my own version of it.
print('How far should I mine?')
local input = tonumber(read())
print('Making ' .. input .. ' block long tunnel...')

for x=1,input do
  if turtle.detectUp() then
    turtle.digUp()
  elseif turtle.detect() then
    turtle.dig()
  end
 turtle.forward()
 print(x)
end

print("Tunnel has been made.")

I have changed the following:
  • io.read() and io.write() into print() and read()
  • Made a for loop instead of a while loop
  • Some other small changes


I hope you get a great time with ComputerCraft! 😀

I ensured that the fuel was well above the amount required and still received the issue. The main issue im seeing is that
1. the tunnel is always a block short
2. If a odd number in imputed then it will not mine the block above it

I do really must thank you for showing me how to get only integer inputs from the user though!

Erb3

QuoteI ensured that the fuel was well above the amount required and still received the issue. The main issue im seeing is that
1. the tunnel is always a block short
2. If a odd number in imputed then it will not mine the block above it

To fix these issues i have edited the code once again.
So inside the loop it looks like this:
if turtle.detect()
  turtle.dig()
end

turtle.forward()

if turtle.detectUp() then
  turtle.digUp()
end

This new code will mine the inputted length forward, no matter if it is a odd number or not.
It's not a data breach, it's a surprise backup.

JarFlow37

Quote from: PC_Cat on Apr 16, 2022, 08:33 AM
QuoteI ensured that the fuel was well above the amount required and still received the issue. The main issue im seeing is that
1. the tunnel is always a block short
2. If a odd number in imputed then it will not mine the block above it

To fix these issues i have edited the code once again.
So inside the loop it looks like this:
if turtle.detect()
  turtle.dig()
end

turtle.forward()

if turtle.detectUp() then
  turtle.digUp()
end

This new code will mine the inputted length forward, no matter if it is a odd number or not.

Thanks so much! It works properly now, who would have guessed the solution was staring right at us?

Terandox

#5
I have compacted and improved your code a bit:
local length = 0
print("How far should i mine?")
io.write("Input Number: ")
length = tonumber(io.read())
for x = 1, length, 1 do
    while turtle.detect() do
        turtle.dig()
    end
    turtle.forward()
    if turtle.detectUp() then turtle.digUp() end
end

The programm should now be able to handle falling blocks like sand or gravel.
In the old version it mined a shorter distance when it ran into these.