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

FarmTurtle program feedback

Started by Themaniacz, 22 June 2013 - 12:26 PM
Themaniacz #1
Posted 22 June 2013 - 02:26 PM
Hi there.
Im new to both ComputerCraft and Lua, and i just wanted to ask you guys what you think about my code.
Ive been reading about lua for about 2 days now, and been making this program as ive learned.

code:

local spot = 0
function move()
print("Moving")
for i=1,8 do
  turtle.dig()
  turtle.select(1)
  turtle.forward()
  turtle.digDown()
  turtle.suckDown()
  turtle.placeDown()
end
end
function main()
for i=1,4 do
  move()
  turnRight()
  move()
  turnLeft()
  sleep(1)
end
end
function turnRight()
if true then
  print("Turning right")
  turtle.select(1)
  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.turnRight()
  turtle.digDown()
  turtle.select(1)
  turtle.placeDown()
end
end
function turnLeft()
if true then
  print("Turning Left")
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
  turtle.digDown()
  turtle.select(1)
  turtle.placeDown()
end
end
function unload()
  num = 1
  if true then
	repeat turtle.back()
	until turtle.detectDown()
	for i=1,16 do
  turtle.select(num)
  turtle.dropDown()
  num = num + 1
	end
	repeat turtle.forward()
	until turtle.detectDown()
  end
end
function takeSeed()
turtle.back()
turtle.suckDown(1,1)
turtle.forward()
end
function homeLine()
turtle.up()
turtle.up()
turtle.turnLeft()
for i=1,spot do
  for i=1,10 do
   turtle.forward()
  end
end
turtle.turnRight()
turtle.down()
turtle.down()
end
function home()
if true then
  turtle.turnLeft()
  for i=1,8 do
   turtle.forward()
  end
  turtle.turnRight()
  for f= 1,9 do
   turtle.back()
  end
end
end
function run()
turtle.forward()
turtle.select(1)
turtle.digDown()
turtle.placeDown()
main()
move()
home()
unload()
end
function nextFarmLine()
turtle.turnRight()
for i=1,10 do
  turtle.forward()
end
spot = spot + 1
end
function runWhat()
print ("What farm number should i move to?")
farmnum = read()
if farmnum == "1" then
  print ("We already here at farm 1")
elseif farmnum == "2" then
  print ("Moving to farm 2")
  turtle.up()
  turtle.up()
  nextFarmLine()
  turtle.down()
  turtle.down()
  turtle.turnLeft()
elseif farmnum == "3" then
  print ("Moving to farm 3")
  turtle.up()
  turtle.up()
  nextFarmLine()
  nextFarmLine()
  turtle.down()
  turtle.down()
  turtle.turnLeft()
else
  print("Not a valid farmnumber")
end
write("To make me farm type run")
print("To take me home type home")
input = read()
if input == "run" then
  takeSeed()
  run()
  homeLine()
elseif input == "home" then
  if farmnum == "1" then
   print("Im already home")
  else
   homeLine()
  end
else
  print("Not working yet")
end
end
runWhat()

Please give me some feedback, and maybe something i could improve within the code or just make it more neat looking.
Thanks in advance.
Themaniacz
Lyqyd #2
Posted 22 June 2013 - 03:30 PM
Split into new topic.
Bomb Bloke #3
Posted 22 June 2013 - 08:49 PM
Indent a couple of spaces not just for your conditional and loop blocks, but for all your functions too. Put empty lines in every now and then - especially at the end of each function block.

This sort of thing:

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

… could be condensed:

for i=1,spot*10 do
   turtle.forward()
end

… or, because the loop is so short, I would write it as a single line:

for i=1,spot*10 do turtle.forward() end

"turtle.forward()" doesn't always result in the turtle moving forward, though, and returns as "false" if it fails. What happens if a mob gets in the way? Consider making a function like this, and calling it instead:

local function goForward()
  while not turtle.forward() do end  -- Continuously try to move forward until "turtle.forward()" returns true.
end

Ditto for "turtle.up()" and "turtle.down()". Failure to check that the movements are successful may result in your turtle getting completely confused about where it is.

See here for some of my jabbering about the purpose of "local". Note that local functions and variables must never be referenced at any point higher in your code then where you first define them.
Themaniacz #4
Posted 23 June 2013 - 05:35 PM
Thanks alot for your feedback Coder.

Still learning stuff, and didn't think about anything could get in the way since my farms are all inclosed in fence. ;)/>

thanks again :)/>