1
\$\begingroup\$

I dont think this will work anywhere besides in minecraft with computercraft but it's all correct syntax. I just feel like it has some lines of code I could eliminated somehow and made it a cleaner script. I appreciate any constructive criticism to help me further understand what I'm misunderstanding.

function placef()
  turtle.select(1)
  x = 1
  if turtle.getItemCount(x) == 0 then
  repeat turtle.select(x+1)
  x = x + 1
  if x == 17 then
  x = 1
  y = 2
  end
  if y == 2 then
  os.reboot()
  end
  until turtle.getItemCount(x) > 0
 end
 turtle.place()
end

function placeup()
  turtle.select(9)
  x = 9
  if turtle.getItemCount(x) == 0 then
  repeat turtle.select(x+1)
  x = x + 1
  if x == 17 then
  x = 9
  y = 2
  end
  if y == 2 then
  os.reboot()
  end
  until turtle.getItemCount(x) > 0
  end
  turtle.placeUp()
end  

function place()
  turtle.select(9)


x = 9
  if turtle.getItemCount(x) == 0 then
  repeat turtle.select(x+1)
  x = x + 1
  if x == 17 then
  x = 9
  y = 2
  end
  if y == 16 then
  os.reboot()
  end
  until turtle.getItemCount(x) > 0
  end
  turtle.placeDown()
end

function repairOT()
  turtle.select(1)
  x = 1
  if turtle.getItemCount(x) == 0 then
  repeat turtle.select(x+1)
  x = x + 1

  if x == 17 then
  x = 1
  y = 2
  end
  if y == 2 then
  os.reboot()
  end
  until turtle.getItemCount(x) > 0
  end
  if turtle.compareDown() == false then
  place()
  turtle.turnRight()
  else
  turtle.turnRight()
  end
  if turtle.compare() == false then
  placef()
  turtle.turnLeft()
  else
  turtle.turnLeft()
  end

turtle.forward()
end

function repairBI()
  turtle.select(1)
  x = 1
  if turtle.getItemCount(x) == 0 then
  repeat turtle.select(x+1)
  x = x + 1
  if x == 17 then
  x = 1
  y = 2
  end
  if y == 16 then
  os.reboot()
  end
  until turtle.getItemCount(x) > 0
  end
  if turtle.compareUp() == false then
  placeup()
  turtle.turnLeft()
  else 
  turtle.turnLeft()
  end
  if turtle.compare() == false then
  placef()
  turtle.turnRight()
  else
  turtle.turnRight()
  end
 turtle.forward() 
end  

These functions are where I think I could do away with alot of lines of code but I'm not sure how. Maybe an anonymous function and a class. Don't really quite understand using those very well yet.

https://pastebin.com/JDZSibmn There is the full script just in case anyone wants to see it. The rest is just loops.

\$\endgroup\$
2
  • \$\begingroup\$ The title is better, yes, but I still don't have the foggiest what problem you're solving. Just when I think I got an idea I see code like os.reboot() in a Minecraft plug-in and I'm all lost again. \$\endgroup\$
    – Mast
    Commented Sep 17, 2018 at 12:30
  • \$\begingroup\$ Yea it has a few Lua apis of Computercrafts. os.reboot() is rebooting the turtle if it can't find an inventory slot with something in it. This is about 2 years old so I didn't have means of it knowing what kind of item it was holding. I think i just need to go rewrite this in the newest version of computercraft. Or just go start learning python or c \$\endgroup\$
    – Donnie
    Commented Sep 17, 2018 at 16:14

1 Answer 1

1
\$\begingroup\$

First of all, use more local. It will save you a lot of headache in the future.

Instead of the three place functions, you could just do

local function selectAnyBlock()
  for i=1,16 -- 4 x 4 inventory
    if turtle.getItemCount(i) > 0 then
      return true
    end
  end
  os.reboot() -- No idea why you'd want to reboot here though
end

and then call turtle.place[up|down]() after that. That saves you two functions and some code.

As for the last two functions, I have no idea what they are supposed to do. They seem to place a block up or down, then turn left and place a block in front?

They also make use of that construct that selects a block in the inventory, so you could replace that with the above selectAnyBlock() function. There's also some places where you have the same instruction (turtle.turnLeft()) in both code paths of a condition. Just place them after the if and you'll only have to write them once.

You don't have to compare booleans to true or false; you can just check for them directly in a condition:

if turtle.compareUp() == false then
  placeup()
  turtle.turnLeft()
else 
  turtle.turnLeft()
end

turns into

if not turtle.compareUp() then
  placeup()
end
turtle.turnLeft()

I don't really see a need for object orientation here. Maybe it makes sense in the program overall, but your example works well with just functions. Same goes for anonymous functions; useful as they are, sometimes you just don't need them.

Overall, try indenting your code like everybody else does (each new scope has its own indentation level) and add some comments to clarify your intentions. This not only helps others reading your code, but also yourself in the future :)

\$\endgroup\$
1
  • \$\begingroup\$ Yea I wasn't positive anything could be done. It just seemed like the best place to get advice and I was having trouble continuing to learn anything with lua. Since I asked this I've started learning python and now I'm asking my self why I wasted so much time on lua. Thanks for the feed back. I'm sure I'll have alot better questions to ask before long. Python is so much easier. \$\endgroup\$
    – Donnie
    Commented Sep 18, 2018 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.