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

My First Attempt at programing (Turtle seed farming)

Started by cdnsailorboy, 11 January 2013 - 12:20 PM
cdnsailorboy #1
Posted 11 January 2013 - 01:20 PM
Hey all, looking for a little help with my first programming attempt. Now I'm sure there are more elegant solutions to this, but I'm mainly just looking to fix my code..if possible. This is basically an attempt to copy functionality that Guude put up a few days ago. Turtle starts in one corner of a 12 X 12 square, moves to the center, places bone meal (located in slot 1 of his inventory), proceeds to the bottom left corner of the square, then "digs" each square row by row.

The problem I'm having is that after he places the bone meal he's jumping a bunch of lines of code and going right into the "turnaround" procedure call. I had original used for loops for all the initial moving around, but as I mentioned above he would jump right to the turnaround procedure call after placing the bone meal, so I elimated them and typed the movements out step by step figuring that I had screwed up a variable somewhere….but even when typing out each movement individually he is still skipping a big chunk…thoughts?
Here's the script. The bold is the part he is skipping.


-- Seed gathering v0.1 by cdnsailorboy

-- Vars
meal = 0
local i = 11
local j = 0
local x = 2
y = 0
dir = {right, left, right, left, right, left, right, left, right, left, right, left}

-- Functions

function row()
while i > 0 do
  turtle.dig()
  turtle.forward()
  i = i - 1
end
i = 11
end

function unload()
turnaround()
x = 2
while x < 17 do
  turtle.select(x)
  turtle.drop()
  x = x + 1
end
turtle.select(1)
end

function turnaround()
turtle.turnRight()
turtle.turnRight()
end

function rowend()
if dir[y] == right then
  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.turnRight()
elseif dir[y] == left then
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
end
end

-- Main Script
meal = turtle.getItemCount(1)
while meal > 0 do
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()

turtle.turnLeft()

turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()

turtle.place()

[b] turtle.forward()[/b]
[b] turtle.forward()[/b]
[b] turtle.forward()[/b]
[b] turtle.forward()[/b]
[b] turtle.forward()[/b]
[b] turtle.forward()[/b]
[b] turtle.forward()[/b]


[b] turtle.turnLeft()[/b]

[b] turtle.forward()[/b]
[b] turtle.forward()[/b]
[b] turtle.forward()[/b]
[b] turtle.forward()[/b]
[b] turtle.forward()[/b]

turnaround()

for y = 1,12 do
  row()
  rowend()
end
unload()
meal = turtle.getItemCount(1)
end

ChunLing #2
Posted 11 January 2013 - 02:09 PM
Heh, bb codes don't work inside of code tags.

So you are farming for seeds and flowers? Flowers I understand, seeds can be obtained much more efficiently by bonemeal farming a single patch of tilled soil.

I believe the problem is that turtles won't move into a space occupied by anything other than air or liquid. So you have tall grass all in front of you and the turtle can't move through it, unless you dig.
cdnsailorboy #3
Posted 11 January 2013 - 03:00 PM
Thanks ChunLing, you were right about the movement…completely forgot that tall grass occupies a block. Modified the code, and now he's planting and getting to the starting point properly. My next problem has been the if statement for the rowend function…basically I want him to turn right when the row counter (y) is odd, and turn left when it's even. I've added a few debuging prints for the y variable when he reaches the end of the row, and it seems to be working correctly, y is incrementing properly, and the value of the fmod is 0 for and even y value, and 1 for and odd value. However, regardless of the value for the math.fmod api he always turns right. Here's the updated code:


-- Seed gathering v0.2 by cdnsailorboy
-- Vars
meal = 0
local i = 11
local x = 2
y = 1
j = 1
-- Functions
function row()
 while i > 0 do
  turtle.dig()
  turtle.forward()
  i = i - 1
 end
i = 11
end
function unload()
 turnaround()
 x = 2
 while x < 17 do
  turtle.select(x)
  turtle.drop()
  x = x + 1
 end
 turtle.select(1)
end
function turnaround()
 turtle.turnRight()
 turtle.turnRight()
end
function rowend()
 if math.fmod(y, 2) == 1 then
  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.turnRight()
 elseif math.fmod(y, 2) == 0 then
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
 end
end
-- Main Script
meal = turtle.getItemCount(1)
while meal > 0 do
for j = 1,5 do
  turtle.dig()
  turtle.forward()
end
turtle.turnLeft()
for j = 1,4 do
  turtle.dig()
  turtle.forward()
end
turtle.place()
turtle.turnLeft()
for j = 1,5 do
  turtle.dig()
  turtle.forward()
end
turtle.turnRight()
for j = 1,7 do
  turtle.dig()
  turtle.forward()
end
turtle.turnRight()
for y = 1,12 do
  row()
  print(y)
  print(math.fmod(y,2))
  sleep(10)
  rowend()
end
unload()
meal = turtle.getItemCount(1)
end

crazyguymgd #4
Posted 11 January 2013 - 03:21 PM
Instead of rowend() use rowend(y). Then change rowend() to

function rowend(diffy)   -- or any other name you want to use
   -- other stuff
   if math.fmod(diffy, 2) == 1 then
   -- your other stuff
end

Then it works for me. I'm been trying to find the wording to explain why this needs to be done but it all just sounds confusing. Basically can't treat that y you use to iterate through the for loop outside of that for loop.
remiX #5
Posted 11 January 2013 - 03:25 PM
It should work .. maybe try the other way of using modulus?



function rowend()
if y % 2 == 0 -- what if you swap it around?
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnLeft()
else -- try using just else?
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnRight()
end
end
cdnsailorboy #6
Posted 11 January 2013 - 03:59 PM
Thanks Crazyguymgd, that got it!

Just so I understand whats going on here, by defining rowend(diffy) you're saying that what ever is within the brackets when rowend is called, gets assigned to the variable diffy within the rowend function?
crazyguymgd #7
Posted 11 January 2013 - 04:05 PM
Thanks Crazyguymgd, that got it!

Just so I understand whats going on here, by defining rowend(diffy) you're saying that what ever is within the brackets when rowend is called, gets assigned to the variable diffy within the rowend function?

Yup. diffy is what's called a parameter of the function rowend. The parameter works just like a local variable declared inside of the function, initialized to the value that you pass in. This is an extremely important concept to start using at early stages instead of hoping any of your local or global variables are defined or correct.