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 :)