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

[LUA][Questions]Counter... or not... [Solved]

Started by Lost Ninja, 18 December 2012 - 01:25 PM
Lost Ninja #1
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.

-- 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
ChunLing #2
Posted 18 December 2012 - 09:12 PM
"echo" is not a function defined anywhere in your code, CC, or standard Lua. It is an identifier associated with the value nil.

Use a debug print to show the values of all the variables you need to track. Or, use global variables and then access them through another program, but that's kinda a hassle.

Test turtle.compareDown() in the lua program. Enter "turtle.select(16)", and if the turtle selects slot 16, place a block there which is identical to the block underneath the turtle and enter "print(turtle.compareDown())". If this outputs "true", then the result is as expected. If it outputs "false", then the turtle doesn't believe that the block under it is identical to the block in slot 16 (grass and dirt are different blocks, for example). If it outputs "nil" or shows an attempt to call nil error, then something is super wrong with your turtle.
Lost Ninja #3
Posted 18 December 2012 - 11:28 PM
Okay thanks for the help, with the I solved the issues I was having.

1)Echo is not the correct way to get something to Print… :/ I must have read and re-read the code about 30 times and missed all the other versions being print and not echo…

2)I copy and pasted my tryDigDown from my tryDig, so it was always failing to dig down due to me telling it to dig forward…

3)Completely changed the testDown() function so that it did all of the testing within the loop, took me a fair few iterations to understand what I was doing wrong.

Code below works as expected, digs out until it finds a block that matches the block in slot 16 (removed the variable temporarily) whereupon it returns to the start point and prints how many blocks it travelled.
Spoiler
-- 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
-- v03
-- by LN

local travld = 0 -- How many blocks we travel
local dist = 0 -- As above
local done = 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.digDown()
        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()
end

local function reverse()
    turtle.turnLeft()
    turtle.turnLeft()
end

local function travel()
while done ~= 1 do
turtle.select(16)
if turtle.compareDown() then
done = 1
-- travld = travld + 1
print("True")
else
done = 0
print("False")
if (done == 0) then
tryDigDown()
tryDig()
tryForward()
travld = travld + 1
end
end
turtle.select(1)
print("Done = " .. done)
end
end

local function rtb()
reverse()
dist = travld
while dist > 0 do
    tryForward()
    dist = dist - 1
end
reverse()
end

-- Program...


travel()
rtb()
print("This Turtle Counted a total of: " .. travld .. " blocks before RTB.")
http://pastebin.com/JuwVDJR3

Thanks for the help again…