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

Stop turtle at certain level

Started by colt_419, 08 August 2013 - 07:36 PM
colt_419 #1
Posted 08 August 2013 - 09:36 PM
I am fairly new to Lua and am having trouble with finding a way to stop my turtles at a certain level.

local fuelSlot = 16
local fuelToConsume = 1
local fuelNeeded = 2
local lastSlot = 14
local blockSlot = 1
local fuel = turtle.getFuelLevel()
while turtle.getItemCount(1) <= 10 do
  print "Please put atleast 10 cobble/dirt in slot 1. Also put atleast 1 GRAVEL in slot 2, if you do not do so the turtle might bug and dig straigt down. Hit any key when done."
  os.pullEvent("char")
end
function gravelCheck()
turtle.select(2)
if turtle.compare(2) == true then
  turtle.dig()
  gravelCheck2()
end
end
function gravelCheck2()
turtle.select(2)
if turtle.compare(2) == true then
  turtle.dig()
  gravelCheck()
end
end
function checkFuel()
  if fuel < fuelNeeded then
   turtle.select(fuelSlot)
   print"Getting Fuel"
   turtle.refuel(fuelToConsume)
   return false
  end
  if fuel >= fuelNeeded then
   turtle.select(fuelSlot)
   print"Already Fueled"
   return true
  end
end
function digDown()

  if fuel >= fuelNeeded then
   turtle.select(blockSlot)
   turtle.digDown()
   turtle.down()
   turtle.placeDown()
  end

end
function digUp()

  if fuel >= fuelNeeded then
   turtle.up()
   turtle.digUp()
   turtle.up()
   turtle.digUp()
   turtle.down()
   turtle.down()
   return true
  end
end
function dig()

  if digUp() == true then
   turtle.dig()
   turtle.forward()
  end
end
function mineDown()
checkFuel()
  if fuel >= fuelNeeded then

  digDown()
  digUp()
  dig()

  end
end


while turtle.getItemCount(1) >= 10 do
  gravelCheck()
  gravelCheck2()
  mineDown()


end





Thats my current Mining Turtle code. I would like it to stop on level 6. Thank you in advanced to whoever can help.
Bubba #2
Posted 08 August 2013 - 10:45 PM
You can go about this one of two ways:

1) Use a positioning program (either GPS or just a "local positioning service")
2) Dig down until you hit bedrock, then dig back up two or three layers (usually you won't go all the way down to layer 1)

For precision and fuel efficiency, I will discuss the first option as it is what I would use.

In order to use GPS, you'll need to enter coordinates manually into four computers that are rednet enabled. Fortunately it's a one time deal if you use a startup program. Take a look at this tutorial for more details on setting up a GPS system.

After you've set up the GPS, getting the turtle down to level 6 would be as simple as:


function gotoLayer(layer)
  while true do
	local x,y,z = gps.locate()
	if y == layer then
	  return true
	elseif layer<y then
	  turtle.digDown() turtle.down()
	elseif layer>y then
	  turtle.digUp() turtle.up()
	end
  end
end

gotoLayer(6)

A local positioning service would be something where you manually enter the coordinates into a turtle, and then use something similar to the above method to get to the correct layer. There are a few turtle navigation systems spread about the forums (check out the programs/turtles section) that you may want to check out, but keep in mind that if you break the turtle and move it elsewhere, you will need to enter all the coordinates again.
colt_419 #3
Posted 08 August 2013 - 11:31 PM
Thank you very much. I have made a few changes, and have implimented the code.

while turtle.getItemCount(1) <= 9 do
  print "Please put atleast 10 cobble/dirt in slot 1. Also put atleast 1 GRAVEL in slot 2, if you do not do so the turtle might bug and dig straigt down. Hit any key when done."
  os.pullEvent("char")
end
local fuelSlot = 16
local fuelToConsume = 1
local fuelNeeded = 2
local lastSlot = 14
local blockSlot = 1
local fuel = turtle.getFuelLevel()
function Dump()
if(turtle.detectUp() == true) then
turtle.select(14)
turtle.digUp()
turtle.placeUp()
elseif(turtle.detectUp() == false) then
turtle.select(14)
turtle.placeUp()
if(turtle.detectUp() == true) then
for i = 3, lastSlot do
turtle.select(i)
turtle.dropUp()
if(i ==14) then
turtle.digUp()
sleep()
end
end
end
end
end
function gravelCheck()
turtle.select(2)
if turtle.compare(2) == true then
  turtle.dig()
  gravelCheck2()
end
end
function gravelCheck2()
turtle.select(2)
if turtle.compare(2) == true then
  turtle.dig()
  gravelCheck()
end
end
function checkFuel()
  if fuel < fuelNeeded then
   turtle.select(fuelSlot)
   print"Getting Fuel"
   turtle.refuel(fuelToConsume)
   checkFuel()
  end
  if fuel >= fuelNeeded then
   turtle.select(fuelSlot)
   print"Already Fueled"
   return true
  end
end
function digDown()

  if fuel >= fuelNeeded then
   turtle.select(blockSlot)
   turtle.digDown()
   turtle.down()
   turtle.placeDown()
  end
 
end
function digUp()

  if fuel >= fuelNeeded then
   turtle.up()
   turtle.digUp()
   turtle.up()
   turtle.digUp()
   turtle.down()
   turtle.down()
   return true
  end
end
function dig()

  if digUp() == true then
   turtle.dig()
   turtle.forward()
  end
end
function mineDown()
checkFuel()
  if fuel >= fuelNeeded then
 
  digDown()
  digUp()
  dig()
 
  end
end
function Mine()
while true do
  
  gravelCheck()
  gravelCheck2()
  mineDown()
  
  end
end
function Main()
  while true do
	    local x,y,z = gps.locate()
	    if y == 6 then
		  Dump()
	    elseif y > 6 then
   Mine()
  elseif y < 6 then
   Dump()
  
	    end
  end
end
Main()
Currently the code is working for me. If you could do me a favor and look over it and tell me if there are any bugs you can see right away I would greatly appreciate it.