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

Digging Program Working Unexpectedly

Started by awsmazinggenius, 17 October 2013 - 05:05 PM
awsmazinggenius #1
Posted 17 October 2013 - 07:05 PM
I have made a simple digging program that will attempt to fetch a turtle location via GPS and if it fails asking the user for manual input of the Y coordinate. It then calculates (Y level - 10) how far it needs to mine until it hits lava layer and will place a water bucket so that you can jump down.

The turtle stops, spews out the "Unexpected Error" message I built into the program multiple times into the console and ends the program if it hits a cave midway and there is no block underneath it.

My code is available here:
pastebin get nKy2QuPX
or
Spoiler


local x, y, z = gps.locate()

if y == nil then
  print("Unable to retrieve location via GPS.")
  print("Please enter Y coordinate:")
  y = io.read()
else
  print("Successfully retrieved coordinates via GPS")
end

blocksdig = y -10
blocksdug = 0

print("Please place fuel in the first slot, and a water bucket in the last.")
print("Digging will commence in 5 seconds.")
sleep(5)

function checkFuel()
  if turtle.getFuelLevel() <= 100 then
    turtle.select(1)
    turtle.refuel(1)
  end
end

while blocksdig > blocksdug do
  if turtle.detectDown() then
    turtle.digDown()
    turtle.down()
    checkFuel()
  else
    turtle.down()
    checkFuel()
  end
  blocksdug = blocksdug + 1
end

if blocksdig == blocksdug then
  turtle.select(16)
  turtle.up()
  turtle.up()
  turtle.placeDown()
end

print("Finished digging.")


Thanks for your help.
campicus #2
Posted 17 October 2013 - 07:10 PM
turtle.detectDown() can only return true or false, so your conditional if should just be:


  if turtle.detectDown() then
    turtle.digDown()
    turtle.down()
    checkFuel()
  else
    turtle.down()
	checkFuel()
  end
  blocksdug = blocksdug + 1
awsmazinggenius #3
Posted 17 October 2013 - 08:16 PM
About to check this, I'm assuming I don't indent checkfuel()?
Bomb Bloke #4
Posted 17 October 2013 - 08:55 PM
The Lua interpreter doesn't care whether you do or don't, but for the sake of human readability it's best not to.
awsmazinggenius #5
Posted 17 October 2013 - 09:00 PM
Oh yeah, my bad. Got confused with Python, which will whine at you if you don't indent correctly. The two languages are so similar.

EDIT:
Edited the script a tad, but the thing won't place the water bucket at the end, however it does dig to lava layer without problems. The script in the original post has been updated.
campicus #6
Posted 17 October 2013 - 10:40 PM
Sorry about the indent, it wasn't indented when I wrote it :s


No idea why your turtle won't place the water sorry :s

Just an aside, it won't matter if your "turtle.digDown()" returns false, so you could just have:


turtle.digDown()
turtle.down()
checkFuel()
blocksdug = bocksdug + 1

instead of:

  if turtle.detectDown() then
	turtle.digDown()
	turtle.down()
	checkFuel()
  else
	turtle.down()
	checkFuel()
  end
  blocksdug = blocksdug + 1
awsmazinggenius #7
Posted 18 October 2013 - 08:01 PM
Thanks, just saw no point in having the turtle swing the pickaxe when it doesn't need to and waste time (or does it?). As I said, I'm a noob.