Posted 18 December 2012 - 03:13 PM
Hello
First thing I have attempted to code in Lua and first time playing with turtles! Awesome so far.
Anyway I am trying to write a program for the turtle which will given a set width and length place a torch every 8 blocks.
My testing has been a 20x20 area. In the first row the space between each torch varies and the 3rd 'column' the same happens.
This is the code:
It could be horrific and I may be looking at this the wrong way but any help would be much appreciated. If needs be I can cut out the code that waits for more coal/torches to make it a little cleaner?
Thanks
First thing I have attempted to code in Lua and first time playing with turtles! Awesome so far.
Anyway I am trying to write a program for the turtle which will given a set width and length place a torch every 8 blocks.
My testing has been a 20x20 area. In the first row the space between each torch varies and the 3rd 'column' the same happens.
This is the code:
local torches = turtle.getItemCount(2)
local tArgs = {...}
local width = tonumber(tArgs[1])
local length = tonumber(tArgs[2])
local turnRight = true
local curSlot = 16
function checkFuel(curSlot)
if turtle.getFuelLevel() < 1 and turtle.getFuelLevel() ~= "unlimited" then
turtle.select(1)
if turtle.refuel(1) then
print("Refueling...")
sleep(1)
return true
else
return false
end
turtle.select(curSlot)
return nil
end
return true
end
-- forward function partially taken from SethBling's video
-- http://www.youtube.com/watch?v=YiNFZNBAF5w
function forward(curSlot)
for i=1,10 do
if turtle.detect() then
turtle.dig()
end
if turtle.forward() then
return true
end
end
return nil
end
function prevBlockSlot(curSlot)
for slot=curSlot,2,-1 do
turtle.select(slot)
if (turtle.compareTo(2) and turtle.getItemCount(slot) > 0) or turtle.getItemCount(slot) > 0 then
return slot
end
end
return false
end
function placeDown(curSlot)
if not turtle.compareTo(2) or turtle.getItemCount(curSlot) < 1 then
--if not prevBlockSlot(curSlot) then
--return false
--end
curSlot = prevBlockSlot(curSlot)
end
turtle.turnRight()
turtle.turnRight()
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
return curSlot
end
if width == nil or length == nil then
print("1. Place turtle in left corner")
print("2. Slot 1: Coal")
print("3. Slot 2-16: Torches")
print("4. Type 'torch <width> <length>'")
else
for slot=3,16 do
turtle.select(slot)
if turtle.compareTo(2) then
torches = torches + turtle.getItemCount(slot)
end
end
turtle.select(curSlot)
widthStr = tostring(width)
lengthStr = tostring(length)
print("Lighting up a " .. widthStr .. "x" .. lengthStr .. " platform")
local columns = 1
for j = 1, width, 1 do
for i = 1, length, 1 do
if i < length then
if not checkFuel(curSlot) then
print("Bleep. Bloop. Empty fuel cell.")
while not checkFuel(curSlot) do
sleep(1)
end
print("Bing. All juiced up!")
end
forward()
end
if (i % 9 == 0 and i > 0) or (i == 1 and columns == 1) then
if columns % 9 == 0 or columns == 1 then
if not placeDown(curSlot) then
print("Bleep. Bloop. I lost the torches! Sorry :(/>/>")
while not placeDown(curSlot) do
sleep(1)
end
print("Bing. I found the torches! :D/>/>")
turtle.select(curSlot)
placeDown(curSlot)
end
end
end
end
if j < width then
if turnRight == true then
turtle.turnRight()
forward()
turtle.turnRight()
turnRight = false
columns = columns + 1
else
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turnRight = true
columns = columns + 1
end
end
end
print("I am done master")
end
It could be horrific and I may be looking at this the wrong way but any help would be much appreciated. If needs be I can cut out the code that waits for more coal/torches to make it a little cleaner?
Thanks