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

Mine Shaft Return function issues

Started by Birdgeek3, 04 January 2013 - 11:42 AM
Birdgeek3 #1
Posted 04 January 2013 - 12:42 PM
Hey,

So I am writing a script so the turtle will dig a 2x1 shaft a specified amount of blocks placing a torch every 9 blocks. I got that part working fine but recently I want to the turtle to also turn around and go back to where it began. I am trying to do this all with counting of numbers.

Here is the code
http://pastebin.com/1vJKHK7h
Spoiler

--Variables
local tArgs = { ... }
local togo = tonumber (tArgs[1])
local num = 0
local count = tonumber (tArgs[1])
--Function
function tfuel(amount)
  local fuelLevel = turtle.getFuelLevel()
  if fuelLevel < 50 then
    turtle.select(16)
    turtle.refuel(amount)
    turtle.select(1)
  end
end
function torch()
  if num == 10 then
    turtle.select(15)
    turtle.turnRight()
    turtle.turnRight()
    turtle.forward()
    turtle.placeUp()
    turtle.turnRight()
    turtle.turnRight()
    turtle.forward()
    turtle.select(1)
    num = 0
  end
end
function detect()
turtle.detect()
    if result == true then
	  turtle.dig()
    end
  end
function layer()
  turtle.dig()
  os.sleep(.25)
  detect()
  turtle.up()
  turtle.dig()
  os.sleep(.25)
  detect()
  turtle.down()
  turtle.forward()
end
function turnAround()
  turtle.turnRight()
  turtle.turnRight()
end
--Main Script
print(tArgs[1])
print(togo)
  if togo == (tArgs[1]) then
    repeat
	  tfuel(1)
	  layer()
	  torch()
	  num = num + 1
	  togo = togo - 1
    until togo == 0
  else
    turnAround()
    repeat
	  tfuel(1)
	  turtle.forward()
	  togo = togo + 1
    until togo == (tArgs[1])
  end

The prints are in there for a debug option

-Bird
ChunLing #2
Posted 04 January 2013 - 02:12 PM
Em, yeah. Just change

  if togo == (tArgs[1]) then
	repeat
		  tfuel(1)
		  layer()
		  torch()
		  num = num + 1
		  togo = togo - 1
	until togo == 0
  else
	turnAround()
	repeat
		  tfuel(1)
		  turtle.forward()
		  togo = togo + 1
	until togo == (tArgs[1])
  end
To
repeat
		  tfuel(1)
		  layer()
		  torch()
		  num = num + 1
		  togo = togo - 1
	until togo == 0
	turnAround()
	repeat
		  tfuel(1)
		  turtle.forward()
		  togo = togo + 1
	until togo == (tArgs[1])
Ah, you also might want to change some of those functions. In particular:
function detect()
turtle.detect()
    if result == true then
		  turtle.dig()
    end
  end
The problem here is that result is never true…it is never anything other than nil, because you never assign it a value. I presume you meant to assign it the return from turtle.detect. In that case, this should work:
function detect()
    if turtle.detect() then
		  turtle.dig()
    end
  end
Edited on 04 January 2013 - 01:16 PM
Birdgeek3 #3
Posted 04 January 2013 - 02:27 PM
Em, yeah. Just change

  if togo == (tArgs[1]) then
	repeat
		  tfuel(1)
		  layer()
		  torch()
		  num = num + 1
		  togo = togo - 1
	until togo == 0
  else
	turnAround()
	repeat
		  tfuel(1)
		  turtle.forward()
		  togo = togo + 1
	until togo == (tArgs[1])
  end
To
repeat
		  tfuel(1)
		  layer()
		  torch()
		  num = num + 1
		  togo = togo - 1
	until togo == 0
	turnAround()
	repeat
		  tfuel(1)
		  turtle.forward()
		  togo = togo + 1
	until togo == (tArgs[1])
Ah, you also might want to change some of those functions. In particular:
function detect()
turtle.detect()
	if result == true then
		  turtle.dig()
	end
  end
The problem here is that result is never true…it is never anything other than nil, because you never assign it a value. I presume you meant to assign it the return from turtle.detect. In that case, this should work:
function detect()
	if turtle.detect() then
		  turtle.dig()
	end
  end

I assumed I could test to bool "result" because the turtle api returns the bool "result" if it is true or false, but I guess that would be why gravel still screws it up.

The main fix you suggested worked but the turtle kept going. It did not stop so any clue if I have something causing that?

Here is updated code
Spoiler

--Variables
local tArgs = { ... }
local togo = tonumber (tArgs[1])
local num = 0
local count = tonumber (tArgs[1])
--Function
function tfuel(amount)
  local fuelLevel = turtle.getFuelLevel()
  if fuelLevel < 50 then
    turtle.select(16)
    turtle.refuel(amount)
    turtle.select(1)
  end
end
function torch()
  if num == 10 then
    turtle.select(15)
    turtle.turnRight()
    turtle.turnRight()
    turtle.forward()
    turtle.placeUp()
    turtle.turnRight()
    turtle.turnRight()
    turtle.forward()
    turtle.select(1)
    num = 0
  end
end
function detect()
    if turtle.detect() then
	  turtle.dig()
    end
  end

function layer()
  turtle.dig()
  os.sleep(.25)
  detect()
  turtle.up()
  turtle.dig()
  os.sleep(.25)
  detect()
  turtle.down()
  turtle.forward()
end
function turnAround()
  turtle.turnRight()
  turtle.turnRight()
end

--Main Script
print(tArgs[1])
print(togo)
repeat
    tfuel(1)
    layer()
    torch()
    num = num + 1
    togo = togo - 1
  until togo == 0
  turnAround()
  repeat
    tfuel(1)
    turtle.forward()
    togo = togo + 1
    print (togo)
    print (tArgs[1])
  until togo == (tArgs[1])
ChunLing #4
Posted 04 January 2013 - 03:29 PM
Shoot, I forgot to tonumber(tArg[1]). Such a rookie mistake.

Okay, a numeric for loop is really better for this anyway.
for i=1,togo do
  tfuel(1)
  layer()
  torch()
  num = num + 1
end
turnAround()
for i=1,togo do
  tfuel(1)
  turtle.forward()
  print (i)
  print (togo)
end
Oh, and the result (or return) of a function isn't stored automatically anywhere. You have to use assignment to set a variable equal to the return of a function call. In the case above, I saw no point in storing the return in a variable and then using the return as a conditional, since you can just use a function call in the conditional directly. But if I wanted to have the return in a variable called result, I would have done it like so:
function detect()
  local result = turtle.detect()
  if result == true then
   turtle.dig()
  end
end
If I were going to use the return of turtle.detect more than once in the function, I probably would store it in a variable.
Edited on 04 January 2013 - 02:35 PM
Birdgeek3 #5
Posted 04 January 2013 - 04:01 PM
Shoot, I forgot to tonumber(tArg[1]). Such a rookie mistake.

Okay, a numeric for loop is really better for this anyway.
for i=1,togo do
  tfuel(1)
  layer()
  torch()
  num = num + 1
end
turnAround()
for i=1,togo do
  tfuel(1)
  turtle.forward()
  print (i)
  print (togo)
end
Oh, and the result (or return) of a function isn't stored automatically anywhere. You have to use assignment to set a variable equal to the return of a function call. In the case above, I saw no point in storing the return in a variable and then using the return as a conditional, since you can just use a function call in the conditional directly. But if I wanted to have the return in a variable called result, I would have done it like so:
function detect()
  local result = turtle.detect()
  if result == true then
   turtle.dig()
  end
end
If I were going to use the return of turtle.detect more than once in the function, I probably would store it in a variable.

What does the line
for i = 1,togo 
do?
It doesn't look like the integer i is called or anywhere so I don't get how this counts
ChunLing #6
Posted 04 January 2013 - 04:40 PM
Numeric for loops are basically a complement to iterative function for loops. They are equivalent in function to a while loop, but with a specially scoped numerical iterator. So:
for i=1,togo do
--some things to iterate
end
is basically equivalent to:
do
  local i = 1
  while i <= togo do
   --same things to iterate
   i = i+1
  end
end
It's just more compact.

You can explicitly set the incrementation value, and it doesn't have to be a positive integer (nor does the initial value have to be 1). In that case you would write "for i = initial,final,increment do" (with initial, final, and increment all being numbers, of course).
Edited on 04 January 2013 - 03:44 PM