Posted 17 May 2013 - 11:24 PM
I've been trying to make a good auto-mining program, and it's almost complete, but it inconsistently malfunctions after filling with items and goes to the home chest like it should, then digs through it and 5 blocks behind it, turns left, and starts tunneling, but I don't know how far it will go.
Here is the code:
Its expected behavior is for the turtle to start at the home chest, dig 32 blocks forward (and the blocks above it), turn, dig 32 blocks back to the home chest (but one block to the right) and so on. If it fills its inventory, it goes back to the home chest and dumps everything in it. If the home chest and its inventory are full, it throws an error to stop the program.
Any help in figuring this out would be greatly appreciated, thanks.
Here is the code:
Spoiler
local tArgs = { ... }
-- Digital Rights system, sorta.
--[[
if os.getComputerID() ~= 1 then
if #tArgs == 1 then
if tArgs[1] == "pass" then
if read("") ~= "jpzg" then
error()
end
else error() end
else error() end
end
]]
---- Variable declarations ----
Log = fs.open("log","a") -- Opens a log file for writing output - I use it for debugging
size = tonumber(tArgs[1])
dir = 0 -- 0 is forward, 1 is right, 2 is back, -1 is left ** relative to starting direction **
pos = {0,0} -- x,y where x is distance in front of start and y is distance to the side (right = positive, left = negative)
lPos = pos -- Used to return to the last point where it mined
lastDir = dir -- Used when returning to the last point it mined to make sure its going the same direction
blocksMined = 0
emptying = false -- Set to true to make sure it doesnt call emptyInventory over and over while moving
---- Function declarations ----
left = function() -- Helps keep track of turtles direction
dir = dir -1
if dir == -2 then
dir = 2
end
turtle.turnLeft()
end
right = function()
dir = dir + 1
if dir == 3 then
dir = -1
end
turtle.turnRight()
end
move = function(dist) -- Moves the turtle forward for dist blocks. Also digs the blocks in front of it and above it, keeps track of the turtles position,
local x = 0 -- and empties its inventory if it is full.
while x < dist do
x = x + 1
if turtle.detect() then
turtle.dig()
blocksMined = blocksMined + 1
end
if turtle.forward() == false then
x = x - 1
end
if dir == 0 then
pos[1] = pos[1] + 1
elseif dir == 2 then pos[1] = pos[1] - 1
elseif dir == 1 then pos[2] = pos[2] + 1
elseif dir == -1 then pos[2] = pos[2] - 1
end
if turtle.detectUp() then
turtle.digUp()
blocksMined = blocksMined + 1
end
if emptying == false then
emptyInventory()
end
end
end
goToPoint = function(c) -- Moves the turtle to c using move() where c is {x,y}
print("Position: "..pos[1]..","..pos[2])
Log.writeLine("Position: "..pos[1]..","..pos[2])
if dir == 0 then
if c[2] > pos[2] then
right()
elseif c[2] < pos[2] then
left()
end
elseif dir == 1 then
if c[2] < pos[2] then
left()
left()
end
elseif dir == 2 then
if c[2] > pos[2] then
left()
elseif c[2] < pos[2] then
right()
end
elseif dir == -1 then
if c[2] > pos[2] then
left()
left()
end
end
move(math.abs(pos[2]-c[2]))
if dir == 1 then
if c[1] < pos[1] then
right()
elseif c[1] > pos[1] then
left()
end
elseif dir == -1 then
if c[1] < pos[1] then
left()
elseif c[1] > pos[1] then
right()
end
end
move(math.abs(pos[1]-c[1]))
-- Turn to be facing forward
if dir == 2 then
left()
left()
end
print("Destination: "..c[1]..","..c[2])
Log.writeLine("Destination: "..c[1]..","..c[2])
end
openSlots = function() -- Finds if the turtle has open slots or not
for s = 1,16,1 do
if turtle.getItemCount(s) == 0 then
return true
end
end
return false
end
CopyVector = function(original) -- Used to copy pos to lPos
local new = {}
for k,v in pairs(original) do
new[k]=v
end
return new
end
emptyInventory = function() -- uses openSlots to find if the turtles inventory is full. If it is, it goes to its starting point and puts everything in a chest. Otherwise,
if openSlots() == false then -- returns true. Also stops the program if the chest is full.
lastDir = dir
lPos = CopyVector(pos)
Log.writeLine("Last position: "..lPos[1]..","..lPos[2])
emptying = true
print("Inventory full")
Log.writeLine("Inventory full")
goToPoint({0,0})
Log.writeLine("Last position: "..lPos[1]..","..lPos[2])
print("Dropping off items...")
Log.writeLine("Dropping off items...")
left()
left()
for n = 1,16,1 do
turtle.transferTo(n)
if turtle.drop() == false then
print("\nStorage full. Ending mining")
Log.writeLine("\nStorage full. Ending mining")
print("Mined "..blocksMined.." blocks")
Log.writeLine("Mined "..blocksMined.." blocks")
error()
end
end
Log.writeLine("Last position: "..lPos[1]..","..lPos[2])
left()
left()
print("Returning to mining")
Log.writeLine("Returning to mining")
Log.writeLine("Last position: "..lPos[1]..","..lPos[2])
goToPoint(lPos)
emptying = false
while dir ~= lastDir do
left()
end
else
return true
end
end
--[[-- Debug Stuff ----
goToPoint({5,17})
lPos = CopyVector(pos)
sleep(3)
goToPoint({0,0})
sleep(3)
goToPoint(lPos)]]
---- Main Program ----
-- Digs a 32xinfinityx2 passage until the turtles home chest is full
while true do
move(32)
right()
move(1)
right()
move(32)
left()
move(1)
left()
end
Its expected behavior is for the turtle to start at the home chest, dig 32 blocks forward (and the blocks above it), turn, dig 32 blocks back to the home chest (but one block to the right) and so on. If it fills its inventory, it goes back to the home chest and dumps everything in it. If the home chest and its inventory are full, it throws an error to stop the program.
Any help in figuring this out would be greatly appreciated, thanks.