Posted 16 October 2012 - 10:26 AM
Newish user of CC here, done a bit of programming in the past some of it with lua so can read the structure of a program even if I don't quite get what is going on 100%. Been playing with the default (included) Tunnel program trying to get a couple more features into it. Specifically a RTB feature that in the original is commented out. I got it to work but am having issues with fueling. Can anyone give me some help?
((Apologies for all the eroneous comments they're mostly to make it clear to me what I'm doing…))
http://pastebin.com/xJ7VMe40
I thought by using the tryForward function that would handle the fuelling as it does in the rest script but it still runs out of fuel. Next I figured I could just call the refuel function (which works) but if the turtle is either a long way off (100+) it still stops short of the full RTB, while if it's close it wastes the fuel when it is inevitably broken to reposition.
While any help is of course appreciated I'd really appreciate if you don't suggest adding an additional API because I'd much rather get it working with just the code you start out with.
Spoiler
-- A revised version of the built in tunnelling script that mines a 3x3 tunnel (as opposed to 3x2)
-- Fills in any missing floor blocks with blocks from slot 1 (in theory)
-- returns to start point (buggy won't refuel)
-- Mk III v0.1
-- changes by LN
local tArgs = { ... }
if #tArgs ~= 1 then -- No length supplied error out with message
print( "Usage: tunnel <length>" )
return
end
-- If value supplied check to make sure it's positive
local length = tonumber( tArgs[1] )
if length < 1 then
print( "Tunnel length must be positive" )
return
end
-- some more variables (wtf is depth?)
local depth = 0
local collected = 0
local dbg = 0 -- change to 1 if you want test messages to be displayed (LN)
local function collect() -- function works out how much shit you've collected (fails with lapis & redstone iirc)
collected = collected + 1
if math.fmod(collected, 25) == 0 then
print( "Mined "..collected.." items." )
end
end
local function test(txt) -- function to print test/debug messages, bit spammy but shows what turtle thinks it's doing... even when it isn't... :D/>/> (LN)
if dbg == 1 then
print("Turtle says: " , txt )
sleep(5) -- change to shorter/longer to speed up/slow down debug
end
end
local function reverseTurtle() -- 180 degree turn (LN)
turtle.turnRight()
turtle.turnRight()
test("Reversing Turtle")
end
local function tryDig()
while turtle.detect() do
if turtle.dig() then
collect()
sleep(0.5)
test("Dig Fwd")
else
test("Dig Fwd Fail")
return false
end
end
return true
end
local function tryDigUp()
while turtle.detectUp() do
if turtle.digUp() then
collect()
sleep(0.5)
test("Dig Up")
else
test("Dig Up Fail")
return false
end
end
return true
end
local function tryDigDown()
while turtle.detectDown() do
if turtle.digDown() then
collect()
sleep(0.5)
test("Dig Down")
else
test("Dig Down Fail")
return false
end
end
return true
end
local function refuel()
local fuelLevel = turtle.getFuelLevel()
if fuelLevel == "unlimited" or fuelLevel > 0 then
return
end
local function tryRefuel()
for n=1,16 do
if turtle.getItemCount(n) > 0 then
turtle.select(n)
if turtle.refuel(1) then
turtle.select(1)
return true
end
end
end
turtle.select(1)
return false
end
if not tryRefuel() then
print( "Add more fuel to continue." )
while not tryRefuel() do
sleep(1)
end
print( "Resuming Tunnel." )
end
end
local function tryUp()
refuel()
while not turtle.up() do
if turtle.detectUp() then
if not tryDigUp() then
test("Move Up & Dig")
return false
end
elseif turtle.attackUp() then
collect()
test("Attacking Up")
else
sleep( 0.5 )
end
end
return true
end
local function tryDown()
refuel()
while not turtle.down() do
if turtle.detectDown() then
if not tryDigDown() then
test("Move Down & Dig")
return false
end
elseif turtle.attackDown() then
collect()
test("Attacking Down")
else
sleep( 0.5 )
end
end
return true
end
local function tryForward()
refuel()
while not turtle.forward() do
if turtle.detect() then
if not tryDig() then
test("Move Fwd & Dig")
return false
end
elseif turtle.attack() then
collect()
test("Attacking Fwd")
else
sleep( 0.5 )
end
end
return true
end
print( "Tunnelling..." )
--[[ Tunnel cross section
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
Turtle starts at 7, digs 4, moves 4, digs 1, moves 1, digs 2, moves 2, digs 5 & 3, moves 3, digs 6, moves 6, 9 -> 9, 8 -> 8... and back to 7. Places blocks below at 7, 9 , 8. (in that order)
]]
for n=1,length do
turtle.placeDown()
test("Place A")
tryDigUp()
tryUp()
tryDigUp()
tryUp()
turtle.turnRight()
test("Turning Right")
tryDig()
tryForward()
tryDigDown()
tryDig()
tryForward()
tryDigDown()
tryDown()
tryDigDown()
tryDown()
reverseTurtle()
test("Reversing direction faced")
tryDig()
turtle.placeDown()
test("Place Down C")
tryForward()
turtle.placeDown()
test("Place Down B")
tryForward()
turtle.turnRight()
test("Turning Right")
if n<length then
tryDig()
if not tryForward() then
print( "Aborting Tunnel." )
break
end
else
print( "Tunnel complete." )
print( "Returning to start..." )
-- Return to where we started
-- This section is not 100% correct. It should fuel when low on fuel and doesn't, though the tryForward works everywhere else.
-- Does work if the fuel doesn't run out, could just add a manual refuel but that would mean a)in long distances it'd still run out, b)in short dsiatnces I waste fuel... :P/>/>
reverseTurtle()
while length > 0 do
if tryForward() then
length = length - 1
else
tryDig()
end
end
reverseTurtle()
end
end
print( "Tunnel complete." )
print( "Mined "..collected.." items total." )
http://pastebin.com/xJ7VMe40
I thought by using the tryForward function that would handle the fuelling as it does in the rest script but it still runs out of fuel. Next I figured I could just call the refuel function (which works) but if the turtle is either a long way off (100+) it still stops short of the full RTB, while if it's close it wastes the fuel when it is inevitably broken to reposition.
While any help is of course appreciated I'd really appreciate if you don't suggest adding an additional API because I'd much rather get it working with just the code you start out with.