Computercraft mining turtle script with variable maxfuel

--Script by victorqyu aka. sol
--11/8/13
--For Computercraft for Minecraft
--For mining turtle.
--Requires pre-fueling. (The more the better but 64 coal is adequate)
--
--running arguments are x,y,z where;
--x = Number of cycles to move forward. (A cycle consists of 3 blocks on the axis of facing where the turtle occupies the central block) eg 10 cycles = 30 blocks
--y = the number of lines to mine to the left of the turtle.
--z = Max depth to dig down
--n = maxFuel value - Value over which coal and other fuels will not be consumed prior to dumping inventory.
--Enderchest to store items in must be placed in the turtle's last inventory slot
--

local tArgs = { ... }
if #tArgs ~= 4 then --If number of args does not equal this, print useage scirpt
print( "Usage: mineAll <# cycles forward> <# lines to the left> <Depth to dig> <Max fuel buffer(7000 recomended)>" )
return --Ends the program
end

-- VAR DEC's
local fwdCycleTarget = tonumber(tArgs[1]) --retrieve blocks to dig forward of turtle
local lftCycleTarget = tonumber(tArgs[2]) --retrieve blocks to dig left of turtle
local digDepth = tonumber(tArgs[3]) --retrieve depth to dig down
local maxFuel = tonumber(tArgs[4]) --Retrieve fuel buffer
local enoughFuel = false
local keepDigging = true
local oddRowNum = false
local miningComplete = false
local yCurPos = 0
local fwdCycleCurrent = 0
local lftCyclePos = 0
-- VAR DEC's

function checkFuel() --wait for turtle to have enough fuel to complete a cycle.
print("Checking fuel level...")
enoughFuel = false
while enoughFuel == false do
if turtle.getFuelLevel() > (7+digDepth*2) then
enoughFuel = true
else
print("Inadequate fuel to cycle")
print("Please place a fuel source in me.")
sleep(5)
shell.run("refuel all")
end
end
end

function dumpInvToChest() --place all items in inv into enderchest (retrieved from inv slot 16)
print("Clearing inventory...")
turtle.dig()
if turtle.getFuelLevel() < maxFuel then
shell.run("refuel all")
end
turtle.select(16)
turtle.place()
sleep(1)
for i = 1,15 do
turtle.select(i)
turtle.drop()
end
turtle.select(16)
turtle.dig()
end

function digCycle() --Dig one cycle & move into pos for next & dump inv to enderchest
print("Begining dig cycle...")
keepDigging = true
yCurPos = digDepth
--Diging part of cycle
while keepDigging == true do
turtle.digDown()
turtle.dig()
if turtle.down() == false then
turtle.dig()
keepDigging = false
end
if keepDigging == true then
yCurPos = yCurPos-1 --If turtle moved down, lower the Y height var by 1
end
if yCurPos == 0 then
keepDigging = false
end
end
--Return part of cycle
turtle.dig()
turtle.turnLeft()
turtle.turnLeft()
while yCurPos < digDepth do
turtle.dig()
turtle.digUp()
turtle.up()
yCurPos = yCurPos+1
end
--call the dump to enderchest func. to clear inv.
turtle.turnLeft()
turtle.turnLeft()
dumpInvToChest()
end

function movePos() --Move into the next position
print("Moving position...")
if fwdCycleCurrent+1 < fwdCycleTarget then --move up if we haven't met cycle count
turtle.dig()
turtle.forward()
turtle.dig()
turtle.forward()
turtle.dig()
turtle.forward()
fwdCycleCurrent=fwdCycleCurrent+1
elseif lftCyclePos+1 < lftCycleTarget and oddRowNum == false then --Get into pos for return line
turtle.dig()
turtle.forward()
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnLeft()
turtle.dig()
turtle.forward()
lftCyclePos = lftCyclePos+1
fwdCycleCurrent = 0
oddRowNum = true
elseif lftCyclePos+1 < lftCycleTarget and oddRowNum == true then --this part is for the return line
turtle.dig()
turtle.forward()
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnRight()
turtle.dig()
turtle.forward()
lftCyclePos = lftCyclePos+1
fwdCycleCurrent = 0
oddRowNum = false
else
miningComplete = true
print("Mining Complete.")
end
end

shell.run("refuel all") --Initial Fuel up

while miningComplete == false do
checkFuel()
digCycle()
movePos()
end

No comments:

Post a Comment