Posted 18 December 2012 - 11:09 AM
I created program to dig a 5x5 tunnel that will go however many blocks you want long. At every 10 blocks, it should place a torch in the middle of the tunnel and at every 50 blocks it should place a chest and empty its inventory and continue along. It works fine for the first two rows of blocks forward, but after that, it bugs out and starts doing weird things, so I haven't been able to test the torch and chest placements. Here's the code (Sorry for the size! I wanted to be as thorough as possible!):
Just to help, slot 1 in inventory is torches, slot 2 is a block that will be placed down, slot 15 is where the chests will be, and slot 16 is where the fuel source is going. Also, any places where I could make the code smaller would be greatly appreciated! Thanks!
EDIT: Silly me! I figured out the problem. The code works as I want, but it was running out of fuel and not making it back to the beginning of the for loop before it ran out of fuel and would just start back up where it left off. I updated code for anybody who would like to use this!
Just to help, slot 1 in inventory is torches, slot 2 is a block that will be placed down, slot 15 is where the chests will be, and slot 16 is where the fuel source is going. Also, any places where I could make the code smaller would be greatly appreciated! Thanks!
--Variables
turtle.select(1)
local tArgs={...}
local togo=tonumber(tArgs[1])
togo=togo or 1
print ("Making tunnel!")
--Functions
function tfuel()
turtle.select(16)
turtle.refuel()
turtle.select(1)
end
function digRightFirst()
d = 1
repeat
if turtle.detect() then
repeat
turtle.dig()
sleep(1.0)
until turtle.detect()==false
turtle.forward()
if turtle.detectDown() then
sleep(0.25)
else
turtle.select(2)
turtle.placeDown(1)
end
else
turtle.forward()
if turtle.detectDown() then
sleep(0.25)
else
turtle.select(2)
turtle.placeDown(1)
end
end
d = d + 1
until d > 4
end
function moveUp()
if turtle.detectUp() then
repeat
turtle.digUp()
sleep(1.0)
until turtle.detectUp()==false
turtle.up()
else
turtle.up()
end
function dig()
j = 1
repeat
if turtle.detect() then
repeat
turtle.dig()
sleep(1.0)
until turtle.detect()==false
turtle.forward()
else
turtle.forward()
end
until j > 4
end
function digRightFinal()
b = 1
repeat
if turtle.detect() then
repeat
turtle.dig()
sleep(1.0)
until turtle.detect()==false
turtle.forward()
else
turtle.forward()
end
b = b + 1
until b > 4
end
turtle.turnRight()
turtle.turnRight()
end
function turnaround()
turtle.turnRight()
turtle.turnRight()
end
function inChest()
turtle.select(15)
turtle.drop()
r = 3
repeat
turtle.select(r)
turtle.drop()
r = r + 1
until r > 14
end
function down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
end
function forward2()
turtle.forward()
turtle.forward()
end
function reset()
turtle.forward()
turtle.forward()
turtle.turnRight()
end
--Main
for i = 1, togo do
tfuel()
turtle.dig()
turtle.forward()
turtle.turnRight()
digRightFirst()
moveUp()
dig()
moveUp()
dig()
moveUp()
dig()
moveUp()
digRightFinal()
turnaround()
down()
forward2()
if togo >= 10 then
if(i % 10 == 0) then
turtle.turnLeft()
turtle.place()
if(i % 50 == 0) then
turtle.up()
inChest()
turtle.select(1)
end
turtle.turnRight()
end
end
reset()
end
--Returning to start
turnaround()
for q = 1, togo do
tfuel(1)
turtle.forward()
end
turnaround()
turtle.select(1)
EDIT: Silly me! I figured out the problem. The code works as I want, but it was running out of fuel and not making it back to the beginning of the for loop before it ran out of fuel and would just start back up where it left off. I updated code for anybody who would like to use this!