Posted 18 December 2012 - 02:25 PM
Hi I'm trying to create a simple turtle program that will move/mine in one direction until it encounters a block below it at which point it will reverse course and return to it's start position were it will echo how many blocks it travelled.
http://pastebin.com/JuwVDJR3
I have two issues, first it doesn't appear to compare what is in slot 16 with anything below it (I have tried both dirt and torches), I tried adding return false/true into:
Secondly my last line echo statement always errors with "Attempt to call nil", I'm guessing this is because it cannot find the travld variable… but surely if it can't find the variable I'd get the error when I try and change it in an earlier function?
Also after I have used ctrl+T to terminate the program (I have a block of bedrock to stop the turtle disappearing off) is there a way to read variables that were in use prior to the termination? (Or a way to store them in the program so that they can be read then?)
All of the individual parts/functions I've used I have used in other programs (or nicked from functional programs) without issue apart from the testDown() function, so in a way all I need is help on that and the echo oddity.
-- Simple program that mines forward along a 1x2 passageway until
-- it detects a block corresponding to the block in (tstslt) then it returns
-- to the start. It then outputs the number of blocks to the required block
-- v01
-- by LN
local travld = 0 -- How many blocks we travel
local dist = 0 -- As above
local retrn = 0 -- when 1 indicates block matching tstslt matched
local job = "Counter"
local tstslt = 16 -- Slot to compare against (1-16)
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..." ..job)
end
end
local function tryDigDown()
while turtle.detectDown() do
turtle.dig()
sleep(0.5)
end
end
local function tryDig()
while turtle.detect() do
turtle.dig()
sleep(0.5)
end
end
local function tryForward()
refuel()
while not turtle.forward() do
if turtle.detect() then
if not tryDig() then
return false
end
elseif turtle.attack() then
else
sleep( 0.5 )
end
end
return true
end
local function testDown() --Test block below turtle and return if matching slot 16
turtle.select(16)
if turtle.compareDown() == true then
retrn = 1
else
travld = travld + 1
tryDigDown()
end
end
local function reverse()
turtle.turnLeft()
turtle.turnLeft()
end
-- Program...
while retrn < 1 do
tryForward()
tryDig()
testDown()
end
-- rtb
reverse()
dist = travld
while dist > 1 do
tryForward()
dist = dist - 1
end
reverse()
echo("This Turtle Counted a total of: " .. travld .. " blocks before RTB.")
((Edited to replace tabs (in code) with spaces for ease of reading))http://pastebin.com/JuwVDJR3
I have two issues, first it doesn't appear to compare what is in slot 16 with anything below it (I have tried both dirt and torches), I tried adding return false/true into:
local function testDown() --Test block below turtle and return if matching slot 16
turtle.select(16)
if turtle.compareDown() == true then
retrn = 1
return true
else
travld = travld + 1
tryDigDown()
end
end
Which does seem to invoke the RTB… but over any block (including air) so I think I have some issue with how the turtle.compareDown works (I looked at the API page on the wiki).Secondly my last line echo statement always errors with "Attempt to call nil", I'm guessing this is because it cannot find the travld variable… but surely if it can't find the variable I'd get the error when I try and change it in an earlier function?
Also after I have used ctrl+T to terminate the program (I have a block of bedrock to stop the turtle disappearing off) is there a way to read variables that were in use prior to the termination? (Or a way to store them in the program so that they can be read then?)
All of the individual parts/functions I've used I have used in other programs (or nicked from functional programs) without issue apart from the testDown() function, so in a way all I need is help on that and the echo oddity.
Edited on 18 December 2012 - 10:15 PM