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

Tree Farm stuck in loop

Started by Thornfoot, 04 June 2015 - 11:57 PM
Thornfoot #1
Posted 05 June 2015 - 01:57 AM
Today I wrote a script for a small tree farm to learn some lua basics and now it is stuck in a loop and I cannot figure out why.
The code is meant to run a tree farm that consists of a turtle with a sapling on 3 of its 4 sides, an enderchest beneath it, and an openblocks vacuum hopper on the side that does not have a sapling. There is a different enderchest in slot 15 that has charcoal in it and slot 16 holds saplings.

--ItemSort
local function itemsort()
  local n = 1
  while n < 15 do
	turtle.select(n)
	local item = turtle.getItemDetail()
	if item then
	  print(item.name)
	  if
		item.name == "minecraft:sapling"
	  then
		turtle.transferTo(16)
	  else
		turtle.dropDown()
	  end
	  n = n + 1
	else
	  n = n + 1
	end
  end
end

--Dig a Tree
local function tree()
  turtle.dig()
  turtle.forward()
  repeat
	turtle.digUp()
	turtle.up()
  until turtle.digUp() == false
  repeat
	turtle.down()
  until turtle.down() == false
  turtle.back()
  sleep(5)
  itemsort()
  turtle.select(16)
  turtle.place()
end

--Refuel from Enderchest
local function chestfuel()
  itemsort()
  end
  print("Checking Fuel Level")
  if turtle.getFuelLevel() < 30 then
	print("Refueling")
	turtle.select(15)
	turtle.placeUp()
	turtle.suckUp()
	turtle.select(1)
	turtle.refuel()
	turtle.select(15)
	turtle.digUp()
  else
	print("Good Enough")
end

--Main Script
while true do
  turtle.select(1)
  local success, front = turtle.inspect()
  if success then
	if
	  front.name == "minecraft:log"
	then
	  tree()
	  end
	elseif
	  front.name == "minecraft:sapling"
	then
	  turtle.turnLeft()
	else
	  turtle.turnLeft()
  end
  chestfuel()
end
KingofGamesYami #2
Posted 05 June 2015 - 02:58 AM
The only thing I can find wrong/strange with your code is your chestfuel function,


local function chestfuel()
  itemsort()
  end --#end declaration of function
  --#everything below is not within the chestfuel() function
  print("Checking Fuel Level")
  if turtle.getFuelLevel() < 30 then
        print("Refueling")
        turtle.select(15)
        turtle.placeUp()
        turtle.suckUp()
        turtle.select(1)
        turtle.refuel()
        turtle.select(15)
        turtle.digUp()
  else
        print("Good Enough")
end --#end if statement

I assume, based upon indentation of your program, you wanted this entire block to be your function.

Additionally, if statments are usually phrased like this:


if <condition> then

elseif <condition> then

else

end

As opposed to

if
  <condition>
then

else

end

It probably doesn't make a difference in performance, but does make it difficult to read.
Thornfoot #3
Posted 05 June 2015 - 03:44 AM
Ok I removed that end and changed the formating based on your suggestion, I tried the program and now it gets stuck in a loop on the chestfuel function.

--ItemSort
local function itemsort()
  local n = 1
  while n < 15 do
    turtle.select(n)
    local item = turtle.getItemDetail()
    if item then
      print(item.name)
      if item.name == "minecraft:sapling" then
        turtle.transferTo(16)
      else
        turtle.dropDown()
      end
      n = n + 1
    else
      n = n + 1
    end
  end
end

--Dig a Tree
local function tree()
  turtle.dig()
  turtle.forward()
  repeat
    turtle.digUp()
    turtle.up()
  until turtle.digUp() == false
  repeat
    turtle.down()
  until turtle.down() == false
  turtle.back()
  sleep(5)
  itemsort()
  turtle.select(16)
  turtle.place()
end

--Refuel from Enderchest
local function chestfuel()
  itemsort()
  print("Checking Fuel Level")
  if turtle.getFuelLevel() < 30 then
    print("Refueling")
    turtle.select(15)
    turtle.placeUp()
    turtle.suckUp()
    turtle.select(1)
    turtle.refuel()
    turtle.select(15)
    turtle.digUp()
  else
    print("Good Enough")
  end
end

--Main Script
while true do
  turtle.select(1)
  local success, front = turtle.inspect()
  if success then
    if front.name == "minecraft:log" then
      tree()
      end
    elseif front.name == "minecraft:sapling" then
      turtle.turnLeft()
    else
      turtle.turnLeft()
  end
  chestfuel()
end
KingofGamesYami #4
Posted 05 June 2015 - 04:25 PM
Given that the chestfuel() function doesn't contain a loop, I highly doubt it's the culprit.

itemsort does have a loop, but it should be ending properly. I would restructure it as a numeric for loop.

Example:

for i = 1, 15 do
  print( i )
end

Your tree function has a small, probably unrelated, issue with it was well:


  repeat
    turtle.digUp() --#attempt to dig up
    turtle.up() --#attempt to go up
  until turtle.digUp() == false --#try to dig up again, but this time noticing whether or not you dug anything

  repeat
    turtle.down() --#attempt to go down
  until turtle.down() == false --#attempt to go down, but notice if the attempt was successful

It will work as intended, however unnecessary turtle calls are made.

I'd do something like this:


while turtle.digUp() do --#while we are digging something
  turtle.up() --#go up
end
while turtle.down() do end --#while we can go down, we go down.

These are minor issues though. I recommend adding print statements to where you think the problem area is, printing variables. This will help you understand what your code is doing wrong (or right).
Thornfoot #5
Posted 05 June 2015 - 10:13 PM
Adding the print statements in helped, I got it working properly now, thank you for helping and for the tips.