Difference between revisions of "Refuel"

From ComputerCraft Wiki
Jump to: navigation, search
(Adding usage note)
m (Wikilinking lava)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
{{Stub}}
 
{{Stub}}
Refuel is a [[Built In Turtle Programs|built in program for turtles]] that will make turtles search their inventory and use any [[fuel]]s they find, up to the limit specified in the command. The limit specifies the number of fuel items to be consumed, not the total amount of fuel, so "refuel 2" could consume two lava buckets (2,000 fuel total), or two wooden shovels (20 fuel total), depending on which items it first comes across in the turtle's inventory.
+
Refuel is a [[Built In Turtle Programs|built in program for turtles]] that will make turtles search their inventory and use any [[fuel]]s they find, up to the limit specified in the command. The limit specifies the number of fuel items to be consumed, not the total amount of fuel, so "refuel 2" could consume two [[lava]] buckets (2,000 fuel total), or two wooden shovels (20 fuel total), depending on which items it first comes across in the turtle's inventory.
  
  
Line 9: Line 9:
 
|code=refuel 10
 
|code=refuel 10
 
}}
 
}}
 +
 +
== Code ==
 +
The refuel program uses the following code:
 +
 +
<code>
 +
local tArgs = { ... }
 +
local nLimit = 1
 +
if #tArgs > 1 then
 +
print( "Usage: refuel [number]" )
 +
return
 +
elseif #tArgs > 0 then
 +
if tArgs[1] == "all" then
 +
nLimit = 64 * 16
 +
else
 +
nLimit = tonumber( tArgs[1] )
 +
end
 +
end
 +
 +
if turtle.getFuelLevel() ~= "unlimited" then
 +
for n=1,16 do
 +
local nCount = turtle.getItemCount(n)
 +
if nLimit > 0 and nCount > 0 and turtle.getFuelLevel() < turtle.getFuelLimit() then
 +
    local nBurn = math.min( nLimit, nCount )
 +
turtle.select( n )
 +
if turtle.refuel( nBurn ) then
 +
    local nNewCount = turtle.getItemCount(n)
 +
    nLimit = nLimit - (nCount - nNewCount)
 +
    end
 +
end
 +
end
 +
    print( "Fuel level is "..turtle.getFuelLevel() )
 +
    if turtle.getFuelLevel() == turtle.getFuelLimit() then
 +
        print( "Fuel limit reached" )
 +
    end
 +
else
 +
    print( "Fuel level is unlimited" )
 +
end
 +
</code>
  
 
== See also ==
 
== See also ==

Latest revision as of 05:32, 11 September 2014

This page is a stub.
Please help us by expanding it.

Refuel is a built in program for turtles that will make turtles search their inventory and use any fuels they find, up to the limit specified in the command. The limit specifies the number of fuel items to be consumed, not the total amount of fuel, so "refuel 2" could consume two lava buckets (2,000 fuel total), or two wooden shovels (20 fuel total), depending on which items it first comes across in the turtle's inventory.


Usage: refuel <amount>


Grid paper.png  Example
Uses up to 10 fuel items
Code
refuel 10



Code

The refuel program uses the following code:

local tArgs = { ... }
local nLimit = 1
if #tArgs > 1 then
	print( "Usage: refuel [number]" )
	return
elseif #tArgs > 0 then
	if tArgs[1] == "all" then
		nLimit = 64 * 16
	else
		nLimit = tonumber( tArgs[1] )
	end
end

if turtle.getFuelLevel() ~= "unlimited" then
	for n=1,16 do
		local nCount = turtle.getItemCount(n)
		if nLimit > 0 and nCount > 0 and turtle.getFuelLevel() < turtle.getFuelLimit() then
		    local nBurn = math.min( nLimit, nCount )
			turtle.select( n )
			if turtle.refuel( nBurn ) then
			    local nNewCount = turtle.getItemCount(n)
    			nLimit = nLimit - (nCount - nNewCount)
    		end
		end
	end
    print( "Fuel level is "..turtle.getFuelLevel() )
    if turtle.getFuelLevel() == turtle.getFuelLimit() then
        print( "Fuel limit reached" )
    end
else
    print( "Fuel level is unlimited" )
end

See also