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

Crafty Turtle Breadbox

Started by xInDiGo, 13 January 2013 - 04:26 PM
xInDiGo #1
Posted 13 January 2013 - 05:26 PM
so this is a script i made for a crafty bot. a crafty bot is a turtle w/ a crafting table. they craft things.

what it does is takes a stack of wheat from the left, makes bread, and then puts it in the box to its right. it'll do this 2 times, giving you 2 stacks of bread! here's the code:


x = 0
while x ~= 2 do
turtle.turnLeft()
turtle.select(1)
turtle.suck()
turtle.select(2)
turtle.suck()
turtle.select(3)
turtle.suck()
turtle.craft(64)
turtle.turnRight()
turtle.turnRight()
turtle.drop(64)
turtle.turnLeft()
x = x + 1
sleep(1.25)
end

the main bug i found was with inconsistent item stacks in the turtle throwing off its inventory & recipe, that's why it takes a stack at a time.
Ulthean #2
Posted 16 January 2013 - 05:18 AM
Seems like a fun program, keep up the good work :)/>

If you want I'm sure it is possible to have the turtle craft just a single piece of bread, or have the user input a number and have the turtle craft that many pieces.
It's quite a bit of work, but might be a good exercise. If you want help with that let me know.
Skullblade #3
Posted 16 January 2013 - 05:23 AM
Seems like a fun program, keep up the good work :)/>

If you want I'm sure it is possible to have the turtle craft just a single piece of bread, or have the user input a number and have the turtle craft that many pieces.
It's quite a bit of work, but might be a good exercise. If you want help with that let me know.
actually Ulthean it would require minimal work he would have to set it up to craft 1 piece of bread and have that inside a while true loop with a counter. the loop would run creating bread until the counter was greater then the number the user inputted.
xInDiGo #4
Posted 16 January 2013 - 05:41 AM
well i wasn't sure how to implement it. when it pulls bread from the chest it pulls it a stack at a time. if i had it only make 25 pieces instead of 64 then the bread would appear in the 4th slot, if i use all 3 stacks up the bread appears in the 3rd slot. the way i have it now is that anything in the 3rd slot after it crafts gets deposited in the bread box. i'm not sure how it would be able to tell if its wheat its depositing or bread. i'd like to make a user defined input but i haven't quite learned that part yet.

thanks for the feed back and suggestions! you guys are great :]
Ulthean #5
Posted 16 January 2013 - 06:00 AM
Skullblade is right, I was overcomplicating things (crafting one piece at a time, with only 3 pieces of wheat in the inventory). Basically, you could try adding a 'select 4' right before where you craft. If I am correct the bread should then always appear in the 4th slot.

Things you would need to add (I added tips to help you get started in the spoilers, it's up to you if you want to read them or not)
  • User defined input: I suggest command line parameters, it's faster and easier. The user could then type 'breadbox 128' and get 128 pieces of bread.
  • Spoiler
    
    -- This will get the number of breads to create from the command line arguments
    args={...}
    numberOfBreads=toNumber(args[1])
    
  • A restocking function (if one slot gets empty get more wheat for that slot)
  • Spoiler
    
    function restock(slotnumber)
      -- STEP 1: Turn to face the chest
      -- STEP 2: Select the right slot
      -- STEP 3: Get some more wheat
      -- STEP 3B: Make sure the stacks are 'big enough'
      -- STEP 4: Turn back to the where you were before
    end
    
    About step 3B: If there is a stack of 10 pieces of wheat in the chest it will only pull 10, and not 64 like you might expect, this may be a problem if you expect it to craft 64 at a time.
    (if you have difficulty understanding what I am trying to say: use the current version of your program, but in the wheat chest have the first slot be only 10 pieces of wheat and see what happens)
  • A choice you have to make is: If you want the turtle to craft 128 breads, do you want him to craft 2 stacks of 64 (since it is faster) or one bread at a time (since it seems more realistic).
Cranium #6
Posted 16 January 2013 - 06:06 AM

-- wheat chest to the left of turtle
-- bread chest to the right of turtle
-- any block behind the turtle
-- empty space in front of turtle
local function resetPos()
  while not turtle.detect() do
    turtle.turnRight()
  end
  turtle.turnLeft()
end
local function fill()
  for i = 1, 3 do
    while turtle.getItemCount(i) < 64 do
	  turtle.select(i)
	  turtle.suck()
	  if turtle.getItemCount(i + 1) > 0 then
	    turtle.select(i + 1)
	    turtle.drop()
	  end
    end
  end
end
local function bake() --lol
  turtle.craft()
  turtle.turnRight()
  turtle.turnRight()
  for i = 1,16 do
    turtle.select(i)
    turtle.drop()
  end
end
while true do
  resetPos()
  fill()
  bake()
end
Bam…
Super awesome, automatic crafting. It should take from a chest to the left, craft the bread, and drop it in the right hand chest. It will then reposition itself.
xInDiGo #7
Posted 16 January 2013 - 06:17 AM
so in the first spoiler, the args={…} is syntax to prompt the turtle to ask for a user defined number? numOfBreads would be a call function for that args? (sorry if my terminology is off) i'm not home atm so i can't test anything, but how would i call it to see what i input? so say i put in 5, now numberofbreads=5, how do i display that number? print(numberOfBreads)? also, whys there the [1]?

for the restock function, would it check if 1, 2, 3. if any of them are it'll select the slot, and grab more wheat. but how do you make it only grab part of a stack and not the whole thing? i get what you mean about it only pulling 10 pieces cause its in the first slot the turtle tries to grab. but if you replaced the 10 stack with 20 stack or 35 stack it'll still just grab the first one it can.

i guess the turtle could always have wheat in 1-3, and if one of the slots is empty, it'll try to get more wheat. if its not empty it will craft as usual, and if i only wanted it to craft 5 pieces but theres 3 stacks in there, i could have it craft and deposit the 4th slot, so long as it always puts the bread in the 4th slot.

it seems the program has a lot of work it still needs, well lots of work for me :P/>

::edit::
Bam…
Super awesome, automatic crafting. It should take from a chest to the left, craft the bread, and drop it in the right hand chest. It will then reposition itself.

i can't tell if that has user defined amounts of bread, but from the looks it does not. it also looks like the bake section will drop everything in its inventory into the left box. sorry if i'm wrong, i'm still learning the basics really &amp; i struggle to understand.
Cranium #8
Posted 16 January 2013 - 06:27 AM
Sorry to throw lots of code at you…
The bake function will drop everything in the right box, since after it crafts, it will turn right twice, then drop things.
It does not have user defined amounts of bread, it will just run automatically, and when it gets wheat in the left hand box, it will automatically craft it to bread. This is just meant to run indefinitely in the background.
xInDiGo #9
Posted 16 January 2013 - 06:36 AM
Sorry to throw lots of code at you…

Don't be sorry! i learn a lot more then reading correct coding then trying over and over to do something when theres been a better different way to do it the whole time. currently my scripts are in simple steps, with very limited while loops. from what i've noticed everybody uses functions for their scripts, so i've been trying to work on that. one thing i don't understand is the "for i =" i see that a lot of the time but what is i actually? i see that in crainums script its looking at inventory? is it just a generic variable people usually assign?
Cranium #10
Posted 16 January 2013 - 06:42 AM
Yeah, ' i ' in this case is just a variable I am declaring for the 'iteration'. Generally, it's easier to make a variable that you would remember while writing, and I usually use ' i ' because it represents the 'iteration'.
But you can use for times = 1, 3 do, and it would be the same thing.

Edit: Here's a nice little tutorial for you on how for loops are used: http://lua-users.org/wiki/ForTutorial
Edited on 16 January 2013 - 05:43 AM
xInDiGo #11
Posted 16 January 2013 - 06:53 AM
Yeah, ' i ' in this case is just a variable I am declaring for the 'iteration'. Generally, it's easier to make a variable that you would remember while writing, and I usually use ' i ' because it represents the 'iteration'.
But you can use for times = 1, 3 do, and it would be the same thing.
Edit: Here's a nice little tutorial for you on how for loops are used: http://lua-users.org/wiki/ForTutorial

thanks for explaining that! and i'll have to give that tutorial a good read its got lots of information on it.


          if turtle.getItemCount(i + 1) > 0 then
		    turtle.select(i + 1)
		    turtle.drop()

in this part, is this checking to see if there are 4 items in the inventory, and if there is, select the 4th slot (+1) and drop it? cause w/ the getitemcount(i + 1) seems like it would count 1 + 1, 2 + 1, 3 + 1. ugh i feel like such a noob (cause i am derp) please don't hate me :[
Ulthean #12
Posted 16 January 2013 - 06:55 AM
I'm not sure if you are familiar with arrays, if not here is a small explanation:

myArray = {2, 7, 5}

This is an array of 3 items: 2,7 and 5. Unlike a normal variable that holds only one value arrays can store multiple values. Now to access them you use the following notation

myArray[1]

This returns the first value stored in the array, in this case: 2. myArray[2] would return 7 and myArray[3] would return 5.



Back to my code:
  • args={…} gets the command line arguments and stores them in an array called args. So if the user would start the program using "breadbox 64 2 lol 9" args would be an array containing 4 values: 64, 2, lol and 9. Now: args[1] gets the first value of the array, in this case: 64.
  • One thing to keep in mind is that args always stores the values as a string (a word). That's why we add the 'tonumber', which turns the word into a number (in the computer's eyes).
  • print(numberOfBreads) would work just fine.
About restocking:
  • You can only suck stacks from a chest, so you can't say turtle.suck(1)
  • What cranium and I suggest is using a while loop: while the stack in inventory slot 1 isn't full: get more wheat from the chest.
  • You do need to keep in mind that if for example there are 60 pieces of wheat already and you suck another stack that the turtle will end up with 64 in this slot and 60 is the next. That's why you need to drop the excess.
  • In your case: suck until all 3 slots are full of wheat and then select slot 4 and drop the excess wheat.
xInDiGo #13
Posted 16 January 2013 - 07:23 AM
I'm not sure if you are familiar with arrays, if not here is a small explanation:

myArray = {2, 7, 5}

This is an array of 3 items: 2,7 and 5. Unlike a normal variable that holds only one value arrays can store multiple values. Now to access them you use the following notation

myArray[1]

This returns the first value stored in the array, in this case: 2. myArray[2] would return 7 and myArray[3] would return 5.
  • args={…} gets the command line arguments and stores them in an array called args. So if the user would start the program using "breadbox 64 2 lol 9" args would be an array containing 4 values: 64, 2, lol and 9. Now: args[1] gets the first value of the array, in this case: 64.
  • One thing to keep in mind is that args always stores the values as a string (a word). That's why we add the 'tonumber', which turns the word into a number (in the

thanks for explaining that! i knew what arrays are, just didn't know how to use them. also, lets say there's 6 things in the inventory, would craniums (i + 1) find the extra items, or only check in the 4th slot for extra items? here's a piece of code i was working on to check for items, if theres none to suck some up in its slots.


x=1,3
while turtle.getItemCount(x) = 0 do
   turtle.select(x)
   turtle.suck()
  end

so with this i'm hoping that the turtle will check 1 - 3, and if its empty it'll select each slot and fill it with a stack. am i getting warmer?
Ulthean #14
Posted 16 January 2013 - 07:37 AM
This is very close. It will suck up some items, not always a stack.
Cranium #15
Posted 16 January 2013 - 07:53 AM
thanks for explaining that! and i'll have to give that tutorial a good read its got lots of information on it.


		  if turtle.getItemCount(i + 1) > 0 then
			turtle.select(i + 1)
			turtle.drop()

in this part, is this checking to see if there are 4 items in the inventory, and if there is, select the 4th slot (+1) and drop it? cause w/ the getitemcount(i + 1) seems like it would count 1 + 1, 2 + 1, 3 + 1. ugh i feel like such a noob (cause i am derp) please don't hate me :[
Actually, what it does, step by step, is this:
  1. It checks the amount of items in the designated slot. In this instance, it is checking if there was an overfill, from the previous slot. getItemCount(i + 1) means the slot number it took in, plus one(hence the overflow).
  2. If it finds anything there, it will select that slot(again, i + 1), and then drop the items back into the chest.
  3. Since there would be no overflow after, it would craft the bread.
Example:

for i = 1, 3 do
  turtle.select(i) --selects 1 through 3
  turtle.select(i + 1) -- selects 2 through 4
end
Hope that explained it better.
xInDiGo #16
Posted 16 January 2013 - 08:20 AM
ah yes very good, i understand now how that worked! you guys are the best for helping out a total stranger with limited knowledge of the whole thing. i wish i could give you guys a star or a +1 rating or something! <3
Cranium #17
Posted 16 January 2013 - 08:37 AM
You can. It's the little button.