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

[Lua] Crafty Turtle Issue with Wheat

Started by Kingdaro, 17 December 2012 - 10:28 AM
Kingdaro #1
Posted 17 December 2012 - 11:28 AM
Having a problem with a farming turtle script. It's supposed to go through and get a row of wheat and melons in a zig-zag fashion, then dig up a row of melons at the end of the field.

That part works perfectly. However, near the end, I wanted to automatically craft bread and throw it in the chest before it, but it's not really working out very nicely.

Here's the part of the script that controls bread crafting:

local total = 0
for i=1, 16 do
  total = total + turtle.getItemCount(i)
end

for i=1, 16 do
  if turtle.getItemCount(i) > 0 then
    turtle.select(i)
    repeat until not turtle.dropDown(1)
  end
end

for slot=1, 3 do
  turtle.select(slot)
  for w=1, math.ceil(total/3) do
    if not turtle.suckDown() then
      break
    end
  end
end

turtle.craft(turtle.getItemCount(1))

My logic with this was to throw out anything that isn't wheat (in slot 1) in the chest in front, then count all of the remaining items, which all should be wheat. Then it drops all of the items down, 1 by 1. After that, it sucks up a third of the dropped items into slots 1, 2, and 3, then crafts it at the end.

The full code:
Spoiler

local function wheat()
  turtle.select(1)
  turtle.digDown()
  turtle.select(2)
  turtle.placeDown()
  turtle.forward()
end

for x=1, 9 do
  for y=1, 6 do
    wheat()
  end

  if x ~= 9 then
    if x%2 == 1 then
      turtle.turnRight()
      wheat()
      turtle.turnRight()
    else
      turtle.turnLeft()
      wheat()
      turtle.turnLeft()
    end
  end
end

local function melon()
  turtle.select(5)
  turtle.digDown()
  turtle.forward()
end

turtle.forward()
turtle.turnLeft()

for i=1, 8 do
  melon()
end
turtle.turnLeft()
melon()

for i=1, 6 do
  turtle.forward()
end

turtle.select(1)
for i=2, 16 do
  if not turtle.compareTo(i) then
    turtle.select(i)
    turtle.drop()
    turtle.select(1)
  end
end

local total = 0
for i=1, 16 do
  total = total + turtle.getItemCount(i)
end

for i=1, 16 do
  if turtle.getItemCount(i) > 0 then
    turtle.select(i)
    turtle.dropDown(1)
  end
end

for slot=1, 3 do
  turtle.select(slot)
  for w=1, math.ceil(total/3) do
    if not turtle.suckDown() then
      break
    end
  end
end

turtle.craft(turtle.getItemCount(1))

for i=1, 16 do
  turtle.select(i)
  turtle.drop()
end

turtle.turnLeft()
turtle.turnLeft()

At the end, it only drops everything on the ground. That or just throws all the wheat in the chest, uncrafted. What am I doing wrong here?
remiX #2
Posted 17 December 2012 - 11:34 AM
In the second for loop in the auto-bread crafting you have "repeat until not turtle.dropDown(1)" but what is it repeating? O.o
Kingdaro #3
Posted 17 December 2012 - 11:37 AM
It just repeats nothing until it can't drop anything anymore. I use it all the time.
KaoS #4
Posted 17 December 2012 - 04:26 PM

for i=1, 16 do
  if turtle.getItemCount(i) > 0 then
    turtle.select(i)
    repeat until not turtle.dropDown(1)
  end
end

right there you are telling it to go through all 16 slots, if they have items toss them out, you do not check if it is wheat first, you should have wheat in the first slot always and then use the code


turtle.select(1)
for i=2,16 do
  if turtle.getItemCount(i)>0 and not turtle.compareTo(i) then
    turtle.select(i)
    turtle.dropDown(turtle.getItemCount(i))
    turtle.select(1)
  end
end
Lyqyd #5
Posted 17 December 2012 - 07:55 PM
Use turtle.transferTo as well, it'll make this sort of thing far simpler.
Kingdaro #6
Posted 18 December 2012 - 02:05 AM
Use turtle.transferTo as well, it'll make this sort of thing far simpler.
Probably should've mentioned that I'm in FTB.


for i=1, 16 do
  if turtle.getItemCount(i) > 0 then
    turtle.select(i)
    repeat until not turtle.dropDown(1)
  end
end

right there you are telling it to go through all 16 slots, if they have items toss them out, you do not check if it is wheat first, you should have wheat in the first slot always and then use the code


turtle.select(1)
for i=2,16 do
  if turtle.getItemCount(i)>0 and not turtle.compareTo(i) then
    turtle.select(i)
    turtle.dropDown(turtle.getItemCount(i))
    turtle.select(1)
  end
end
This should work. Thanks.
KaoS #7
Posted 18 December 2012 - 02:10 AM
This should work. Thanks.

No problem, good luck


Probably should've mentioned that I'm in FTB.

turtle.transferTo also works in FTB… they update fast
Kingdaro #8
Posted 18 December 2012 - 02:14 AM
Probably should've mentioned that I'm in FTB.

turtle.transferTo also works in FTB… they update fast
tHAT WOULD'VE BEEN HELPFUL TO KNOW
theoriginalbit #9
Posted 18 December 2012 - 02:29 AM
sorry, thought u would have known, since i assumed you used the API page to get info on methods you used.
Kingdaro #10
Posted 18 December 2012 - 02:50 AM
I always use the wiki, but I always thought transferTo was in a version later than FTB's. :/
theoriginalbit #11
Posted 18 December 2012 - 03:01 AM
I always use the wiki, but I always thought transferTo was in a version later than FTB's. :/

Ahh ok. lol. should have used the "lua" program to test ;)/>