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

Turtle gravel farm program not processing entire stacks of gravel

Started by Kronimiciad, 11 July 2014 - 12:33 AM
Kronimiciad #1
Posted 11 July 2014 - 02:33 AM
First off: this is my first post on this forum and I am about a week into learning lua and computercraft and turtles. I apologize in advance if I fail to use the right terms or go into sufficient detail. I tried to indent using tab (that being the only way to indent that I know of) on the code blocks but couldn't. I tried to substitute it with space but it is somewhat tedious and I would guess that there is an easier way that I do not know of, or perhaps I'm not pressing it in the right window. Would someone please explain to me what I'm doing wrong there?

I'm trying to make a program for a turtle that will cycle through all of the turtle's inventory slots and place the block and mine it until all of the gravel in the entire inventory gets turned into flint. The problem is, however, that the turtle will not keep placing and mining a stack of gravel in a given inventory slot until all of the gravel has become flint, but instead will place and mine blocks from a stack for awhile then, before very much if any of the gravel is converted into flint, move on to the rest of the inventory slots. The code looks like this:


x = 1
for i = 1,16 do
	turtle.select(x)
	for j = 1,turtle.getItemCount(x) do
		turtle.place()
		turtle.dig()
	end
	x = x + 1
end

I wrote it this way because I wanted to be able to queue more than one stack of gravel at a time. However, I also tried this:


turtle.select(1)
for i = 1,turtle.getItemCount(1) do
	turtle.place()
	turtle.dig()
  end

And I am still running into the same problem. I tried adding another variable, and having the loop add 1 to it each time it ran, and then print the variable at the end to keep track of how many times the loop ran. I found that the loop only ran the original value of the item count, so I'm assuming that the turtle isn't rechecking the item count each time the loop repeats. Can someone please suggest what I should do?
KingofGamesYami #2
Posted 11 July 2014 - 04:22 AM

for i = 1, 16 do
  turtle.select( i )
  local canPlace = true --#you'll see why I did this shortly
  while canPlace and turtle.getItemCount( i ) > 1 do --#while loops will run the bit of code each time, unlike for loops.
   canPlace = turtle.place() --#if the item is not placable, this will detect it and stop the loop.
   turtle.dig()
  end
end

Note: if the turtle's inventory will always start this program with a gravel block in each slot, the check for if it can place is obsolete.

Edit: about the tab issue:
In the upper left corner of the editor is a button that says "Toggle editing mode" if you hover over it. If you switch to plain text editor, you can copy/paste from you're favorite text editor. I recommend Sublime Text 2, since someone wrote a ComputerCraft package for it.
Edited on 11 July 2014 - 02:26 AM
theoriginalbit #3
Posted 11 July 2014 - 05:50 AM
why not just

for i = 1, 16 do
  turtle.select( i )
  --# if there's an item, and we can place it
  while turtle.getItemCount( i ) > 1 and turtle.place() do
    turtle.dig()
  end
end
wieselkatze #4
Posted 11 July 2014 - 08:14 AM
Nah, that wouldn't place the last gravel. Just replace the condition with turtle.getItemCount(i) > 1 with turtle.getItemCount(i) > 0.

I never thought I would ever get to correct you, bit :P/>
Edited on 11 July 2014 - 06:18 AM
Kronimiciad #5
Posted 11 July 2014 - 05:12 PM
 for i = 1, 16 do turtle.select( i ) local canPlace = true --#you'll see why I did this shortly while canPlace and turtle.getItemCount( i ) > 1 do --#while loops will run the bit of code each time, unlike for loops. canPlace = turtle.place() --#if the item is not placable, this will detect it and stop the loop. turtle.dig() end end 
Note: if the turtle's inventory will always start this program with a gravel block in each slot, the check for if it can place is obsolete. Edit: about the tab issue: In the upper left corner of the editor is a button that says "Toggle editing mode" if you hover over it. If you switch to plain text editor, you can copy/paste from you're favorite text editor. I recommend Sublime Text 2, since someone wrote a ComputerCraft package for it.

Thank you very much!