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

Turtle crafting...

Started by jag, 27 September 2012 - 05:39 PM
jag #1
Posted 27 September 2012 - 07:39 PM
So I was wondering, how do you craft with a single crafty turtle?
I know that you can do
turtle.craft()
but I wan't to form the recipe in the turtle…


So I figure that you can do
while not turtle.detectDown() do
  turtle.down() -- get the turtle down to the ground.
end
turtle.up() -- to keep the turtle 1 block up in the air.

-- I got 2 pieces of log, and I want to craft a chest...
-- The log is in the first position in the turtles inventory
turtle.select(1)
turtle.craft(2)

for i = 1, 9 do -- I THINK this will execute 8 times, starting on 1...
  if i == 4 or i == 6 or i == 8 or then i = i + 1 end -- to get the crafting grid for a chest.
  if i ~= 8 then turtle.dropDown(8 - i) end -- drop the stuff below the turtle, unless it's on it's 8th run.
  if i ~= 1 then turtle.select(i) end -- select the correct slot (not on first run)
  if i ~= 8 then turtle.suckDown() end -- pick everything thats below up again, putting it in the expected slot.
end

if not turtle.craft() then
  print("Failed crafting!")
end

-- so the turtle should place wooden planks in this pattern:
-- [ ] [ ] [ ]
-- [ ] N/A [ ]
-- [ ] [ ] [ ]
-- unless someone else picks up the blocks or if extra blocks where placed below
⇈ That script should craft me a chest…
To be honest, I haven't even tried it. :P/>/>
EDIT: When I tried the code it gave me the error "12: unexpected symbol" (line 12 is in the first if statement in the for loop).
EDIT 2: derp derp, there was an extra or before then.
But is this the correct way to craft with a single crafty turtle?

I can't find in the turtle API a way to move items around inside of the turtle's inventory, or like split the stack…

Pastebin: ZatR8kmB

EDIT 3: So I tried to just write it all down, step by step:
Spoiler
turtle.select(1)
turtle.craft()
turtle.dropDown(7)
turtle.select(2)
turtle.suckDown()
turtle.dropDown(6)
turtle.select(3)
turtle.suckDown()
turtle.dropDown(5)
turtle.select(5)
turtle.suckDown()
turtle.dropDown(4)
turtle.select(7)
turtle.suckDown()
turtle.dropDown(3)
turtle.select(9)
turtle.suckDown()
turtle.dropDown(2)
turtle.select(10)
turtle.suckDown()
turtle.dropDown(1)
turtle.select(11)
turtle.suckDown()
turtle.select(1)
turtle.craft()
Aaaaaaaand, surprisingly it works…
The original script got it to this:
[2] [1] [2]
[2] N/A [1]
N/A N/A N/A
and the ugly stepbystep script crafted successfully.
So there must be something wrong with the script.

EDIT 4: I did the same thing (kind of) with a while loop!
Spoiler
while not turtle.detectDown() do
  if not turtle.down() and turtle.getFuelLevel() <= 1 then
	print("Out of fuel... :D/>/>")
  end
end
turtle.up()

-- got 2 log in slot 1
turtle.select(1)
turtle.craft(2)

local runTimes = 0
local runTimes2 = 1

while runTimes ~= 8 do
  runTimes = runTimes + 1
  runTimes2 = runTimes2 + 1
  if runTimes2 == 4 or runTimes2 == 8 or runTimes2 == 6 then runTimes2 = runTimes2 + 1 end
  if runTimes ~= 8 then turtle.dropDown(8 - runTimes) end
  turtle.select(runTimes2)
  if runTimes ~= 8 then turtle.suckDown() end
end

turtle.select(1)

if not turtle.craft() then
  print("Failed crafting!")
else
  print("Successful crafting!")
end
And it works!!
Pastebin: jN7NtaAT

Edit 5: So I made another script, this time using turtle.place() and turtle.dig() instead of turtle.drop() and turtle.suck() (Idea by Cranium! :)/>/> )
Spoiler
-- got 2 log in slot 1
turtle.select(1)
turtle.craft(2)

local z = 0
while turtle.detect() do -- This makes sure that its empty in front of the turtle.
  turtle.turnRight()
  if z ~= 4 then z = z + 1 end
  if z == 4 then
	turtle.select(16)
	turtle.dig()
	turtle.drop()
	turtle.select(1)
  end
end

local x = 0
for i = 1, 8 do
  x = x + 1
  if x == 4 or x == 8 or x == 6 then x = x + 1 end
  turtle.select(1) -- Instead of turtle.drop() and turtle.suck() we use turtle.place() and turtle.dig()
  turtle.place()
  turtle.select(x)
  turtle.dig()
end

turtle.select(1)

if not turtle.craft() then
  print("Failed crafting!")
else
  print("Successful crafting!")
end
And this works even better!
Pastebin: AzveFNuK
Cranium #2
Posted 27 September 2012 - 07:56 PM
Since you have to use the same item to craft, when you craft logs into planks, you will have to place the planks down, select slot 1, pick it up, back to slot with plank stacks, place another log down, select slot 2, etc….
It's kind of annoying, but I hope you get the idea.
jag #3
Posted 27 September 2012 - 08:00 PM
Since you have to use the same item to craft, when you craft logs into planks, you will have to place the planks down, select slot 1, pick it up, back to slot with plank stacks, place another log down, select slot 2, etc….
It's kind of annoying, but I hope you get the idea.
I don't really understand, can you give an example? Just a short example
GopherAtl #4
Posted 27 September 2012 - 08:02 PM
yeah, you've got the basic idea of how to craft correct, but there's some errors with your for loop logic. Lua does for loops a bit differently from most languages.

First, Lua's for loops end on greater-than rather than equal, so your for i=1,9 will loop 9 times, not just 8.

Second, lua effectively "protects" it's loop variable in a for loop. Easiest to explain this with an example.

for i=1,3 do
  if i==2 then i=i+1 end
  print(i)
end

You might expect this to output this:

1
3

but actually it'll output this:

1
3
3
when i==2, it did increment i, but the next time through the loop it still ran with 3 as the value of i, so 3 was output twice.

To do what you're trying to do with i there, you'll want to make a second variable which you increase manually.

:edit to reply to ninjas:
Your example code was doing basically what cranium said, you dropped the items and sucked back into the different slots. There's no simpler way than that, sadly. ALso, I think he's pointing out that the planks you craft from logs in slot 1 will end up in slot 2, because the logs were in slot 1.
Doyle3694 #5
Posted 27 September 2012 - 08:20 PM
if gave you unexpectedsymbol because you said "or then"…. I think you was to make another or but cahnged your mind and forgot to take away that or. Do it… NOW
jag #6
Posted 27 September 2012 - 08:29 PM
if gave you unexpectedsymbol because you said "or then"…. I think you was to make another or but cahnged your mind and forgot to take away that or. Do it… NOW
Yeah, that was exactly what happend, I was planning to add another reason to increase i but I changed my mind :P/>/>
I fixed that
Cranium #7
Posted 27 September 2012 - 08:29 PM
You want to change your for loop to be for i = 1,8 do, but you're close.
Initially, we want two logs to be crafted into 8 planks. so first:

turtle.craft()
Assuming we only had two logs in the turtle, we just bade 8 planks. Next:

turtle.select(2) --select the 8 planks that we made.
turtle.drop() --drop them on the ground, or place them in a chest...Trust me this is easiest.
turtle.select(16) --selects the last slot. You following me, cameraman?
turtle.suck() --takes in all 8 planks into slot 16.
We now have all 8 blocks of wooden planks in slot 16. Now the fun can begin:

local function select(slot)
turtle.select(16) --select the stack of planks
turtle.placeDown() --places a plank down
turtle.select(slot) --selects our defined slot
turtle.digDown() --takes in one plank to defined slot
end
Now that's our function, and now to implement it:
 
select(1) 
select(2) 
select(3) 
select(5) 
select(7) 
select(9) 
select(10) 
select(11)
turtle.craft()
If all went correctly, you should now have one chest in your turtle's inventory.
jag #8
Posted 27 September 2012 - 09:20 PM
You want to change your for loop to be for i = 1,8 do, but you're close. Initially, we want two logs to be crafted into 8 planks. so first:
 turtle.craft() 
Assuming we only had two logs in the turtle, we just bade 8 planks. Next:
 turtle.select(2) --select the 8 planks that we made. turtle.drop() --drop them on the ground, or place them in a chest...Trust me this is easiest. turtle.select(16) --selects the last slot. You following me, cameraman? turtle.suck() --takes in all 8 planks into slot 16. 
We now have all 8 blocks of wooden planks in slot 16. Now the fun can begin:
 local function select(slot) turtle.select(16) --select the stack of planks turtle.placeDown() --places a plank down turtle.select(slot) --selects our defined slot turtle.digDown() --takes in one plank to defined slot end 
Now that's our function, and now to implement it:
 select(1) select(2) select(3) select(5) select(7) select(9) select(10) select(11) turtle.craft() 
If all went correctly, you should now have one chest in your turtle's inventory.

Thanks for the turtle.place() and turtle.dig() usage idea, instead of using turtle.drop() and turtle.suck()!
So I made a new script using this! (in "edit 5")