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

Condense long code into fewer lines for wheat farm

Started by schichtmschacht, 06 August 2013 - 03:58 AM
schichtmschacht #1
Posted 06 August 2013 - 05:58 AM
Running: FTB Ultimate (v.1.1.2) Minecraft Version 1.4.7

I have 45*45 squares of farmland where I want to grow wheat in mass, around 2000 wheat each harvest. For this I want to use a mining turtle and have done so in much smaller scale with very basic code, see below:

turtle.refuel()
turtle.forward()
  turtle.digDown()
  turtle.select(2)
  turtle.placeDown()
   turtle.turnRight()
turtle.forward()
  turtle.digDown()
  turtle.select(2)
  turtle.placeDown()
   turtle.turnRight()
turtle.forward()
  turtle.digDown()
  turtle.select(2)
  turtle.placeDown()

It starts out on the edge of the farm above the wheat, gets fuel, moves forward, harvests the wheat and seeds, places a new seed from slot 2 and turns right. This is from the bend where it is turning around for the next row of wheat. Thise code is copied as needed to match the total length of the field. Obivously this is very basic and labourious procedure and not viable for a field of around 2000 blocks.

So my question is, how can I condense these repeating lines into functions where I can just tell it that the field is 45*45, that it should harvest and place seeds from anywhere in the the turtle inventory, detect when the inventory is full and return to the chest at the starting point to unload but keep a stack of seeds, detect when there is no more wheat beneath it and turn (depending on starting left or right corner of the field) and keep repeating this until it reaches the end of the field (preferrably by detecting cobblestone infront and to the left or infront and to the right depending on starting point).

Thank you in advance, schichtmschacht
GamerNebulae #2
Posted 06 August 2013 - 07:25 PM
If you have some experience with CC, which I assume you don't have reading the code, you know a while-loop would be the best option for you.

Explanation: A while-loop is a loop that checks for a condition before it runs the code. Otherwise the code inside the while-loop will not run and it would go on with the code outside the loop. Let's make it easy and understandable for everyone: It kinda works like your thoughts. Let's say you are hungry and you want to some fruit to eat. You check the fruit bowl and there is an apple laying there. You grab it and eat it. If the apple wasn't there, you would have not eaten it. Just because the conditions were not there.

Let's go over to coding! Seeing you're only doing one thing actually, it would be easy for someone that has been working with lua for longer. I'll walk you through step by step. Note this is only demonstration and not actual code intended to use!


--# before you begin, make sure there is cobblestone in slot 16 (the most bottom right slot)
while true do --# An infinite loop that only will stop if something is false
  turtle.select(16) --# Your cobblestone slot
  if not turtle.compareDown() then
    return false --# It checks if the block BELOW the turtle is cobblestone, if not, it will go on with the next code
  else
    turtle.forward()
    turtle.digDown()
    turtle.select(2)
    turtle.placeDown()
    turtle.turnRight()
  end
end

Hope this helped. If you have ANY questions, feel free to ask me, I will keep an eye on this forum thread!
Kingdaro #3
Posted 06 August 2013 - 07:47 PM
You could probably use a for loop too. Of course, checking if you're on the last turn so you don't .turnRight() again.


turtle.refuel()
for i=1, 3 do
  turtle.forward()
  turtle.digDown()
  turtle.select(2)
  turtle.placeDown()
  if i < 3 then
    turtle.turnRight()
  end
end
GamerNebulae #4
Posted 06 August 2013 - 08:00 PM
You could probably use a for loop too. Of course, checking if you're on the last turn so you don't .turnRight() again.


turtle.refuel()
for i=1, 3 do
  turtle.forward()
  turtle.digDown()
  turtle.select(2)
  turtle.placeDown()
  if i < 3 then
	turtle.turnRight()
  end
end

You could, but you would need to calculate exact timings and stuff. This is more of the lazy way :P/> I didn't get exactly what he was going for, so I just tried to copy it as best as I could.
schichtmschacht #5
Posted 07 August 2013 - 08:36 AM
Good morning and thank you for the quick answers. What you posted as first reply is what I was looking for in terms of example and explanation. Lets define some static conditions:

1. The field itself is 45*45 blocks surrounded by cobblestone
2. The turtle will start out at the left corner of the field (on a cobblestone block), just infront and above of the first wheat block.
3. The loop must end when it detects cobblestone infront and below + either to the left or right below (this can later be clearly defined with right or left depending on where the field ends), that is the end of the field.
4. In between it must return to a chest that is located behind the turtle at starting point to empty its inventory but keeping a stack of seeds, and refuel twice since movement is more than 2000 blocks (lava bucket)

A picture with the layout of the farm and path of the turtle is added to the post.

//schichtmschacht