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

Fuel up your turtle with ease!

Started by leftler, 03 March 2013 - 05:30 PM
leftler #1
Posted 03 March 2013 - 06:30 PM
I wrote this simple little program to drive my turtle over a lake of lava and suck up and refuel itself as it goes. All you need to do is put a empty bucket in the active slot on the turtle, specifiy how many blocks forward you want to travel, and optionally how far to go down (if you have already gotten the first layer, might as well move on to the seccond :)/> )

http://pastebin.com/nFSUKiYE

Spoiler
	------------------------------------------------------------------
--			  Turtle Automatic Lava Collection
--			   By Scott Chamberlain (leftler)
-- Usage: place bucket in inventory and call with the
--  arguments <blocks> [drop]
--
-- The turtle travels 'blocks' forward and sucks all lava as
-- fuel in then makes a U-turn and attempts to retreive lava on
-- the way back. The seccond parameter will tell the turtle to
-- drop that many blocks after moving forward once.
--
-- if the turtle is a mining turtle it will break any blocks
-- blocking it's path
------------------------------------------------------------------

local tArgs = { ... }

if #tArgs == 0 then
  print("Usage: <blocks> [drop]")
  return
end

turtle.refuel()

if turtle.getFuelLevel() == 0 then
  print("The turtle must have at least some fuel to start with.")
  return
elseif turtle.getFuelLevel() == "unlimited" then
  print("This turtle does not use fuel to move")
  return
end

--Number of blocks to travel to get the lava
local maxDistance = tArgs[1]

--How deep to go to gather the lava
local drop = 0
if #tArgs >= 2 then
  drop = tonumber(tArgs[2])
end  

--local counter for how far the turtle has travle
local traveled = 0

--A helper function to try and move forward, it will attempt to dig if it is blocked.
local function moveForward()
  if turtle.forward() == false then
	--Failed to move, see if we can dig our way forward
	while turtle.dig() do
		  --Keep digging till we can't dig any more, in case gravel is falling.
		end
		if turtle.forward() == false then
		  print("I am either blocked or out of fuel.")
		  return false
		end
  end
  return true  
end

--A helper function to try and move down, it will attempt to dig if it is blocked.
local function moveDown()
  if turtle.down() == false then
	--Failed to move, see if we can dig our way down
	turtle.digDown()
		if turtle.down() == false then
		  print("I am either blocked or out of fuel.")
		  return false
		end
  end
  return true  
end

--A helper function to try and move down, it will attempt to dig if it is blocked.
local function moveUp()
  if turtle.up() == false then
	--Failed to move, see if we can dig our way down
	while turtle.digUp() do
		  --Keep digging till we can't dig any more, in case gravel is falling.
		end
		if turtle.up() == false then
		  print("I am either blocked or out of fuel.")
		  return false
		end
  end
  return true  
end

dropDone = false

--Attempt to travel to max distance for getting lava
for i = 1, maxDistance do
  if moveForward() == false then
	print("Could not move forward the full requested distance, starting U-Turn early.")
	   break
  end
  traveled = traveled + 1

  --drop to the requested depth
  if(dropDone == false) then
	for j = 1, drop do
		  if moveDown() == false then
			print("Could not drop the full distance")
				drop = j - 1
				end
		end
		dropDone = true
  end

  --grab the fule
  turtle.placeDown()
  if turtle.refuel() == false then
	--Whatever we picked up is invalid for fuel, put it back down
	turtle.placeDown()
  end
end

--Turn around move a block over and get more lava
turtle.turnRight()
moveForward()
turtle.turnRight()

--Travel back the same number of blocks we came
for i = 1, traveled do
  turtle.placeDown()
  turtle.refuel()

  --Check to see if we need to go up yet
  if i == traveled then
	for j = 1, drop do
		  moveUp()
		end
  end

  if moveForward() == false then
   --We are stuck, attempt to go up a level to get home
	   if drop > 0 then
		 repeat
		   moveUp()
		   drop = drop - 1
			   lastMove = moveForward()
		  until lastMove == true or drop == 0
	  
		 if lastMove == false then
			print("I am stuck!")
		 end
	  end
  end
end
  
turtle.turnRight()
moveForward()
turtle.turnRight()


[media]http://www.youtube.com/watch?v=8fJO7R7nwxE[/media]

The bug at the end of the video was caused by me standing in the way of the turtle so it attempted to "go above me" to get out of the way of the blockage, had it been a mining turtle it would have just kept swinging it's pickaxe at me.
JustPingo #2
Posted 04 March 2013 - 03:48 AM
Good program.
It's useful if you have a Excavation turtle which it found a lava lake to refuel itself.
Habibi #3
Posted 06 March 2013 - 09:08 AM
Good program.
It's useful if you have a Excavation turtle which it found a lava lake to refuel itself.

How do you combine those?
JustPingo #4
Posted 06 March 2013 - 10:05 AM
Each times, test if block under it is a lava source (with the bucket) and if it is true, refuel.

Else, you can check yourself if there is a lava lake, and run the program if it is true.
Habibi #5
Posted 06 March 2013 - 10:21 AM
Each times, test if block under it is a lava source (with the bucket) and if it is true, refuel.

Else, you can check yourself if there is a lava lake, and run the program if it is true.

I am very bad at editing, Can you possibly do it?
lionzNtiggerz #6
Posted 06 March 2013 - 09:07 PM
Cool. My turtles always run out of fuel too easy
Kris #7
Posted 31 March 2013 - 10:33 PM
Can you combine or lounch this program togheter with other quarry programms ?
NomNuggetNom #8
Posted 13 June 2013 - 02:22 PM
Awesome! Thanks.
Somin #9
Posted 14 June 2013 - 02:01 AM
Nice~ What I did is I have a big tank of laval and a liquiduct coming out. The turtle is able to refuel using the liquiduct and a tank peripher from misc peripherals. Was pretty easy to do and now whenever I refuel i type 'fuel 1000000' and end up with 1 million fuel in the turtle. works great~