This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
lucifael1975's profile picture

Mining Script Help Requests

Started by lucifael1975, 28 July 2012 - 02:40 AM
lucifael1975 #1
Posted 28 July 2012 - 04:40 AM
I've got a mining program I've been working with for a while now and I am a little stuck on something, so far it's come along way from the tunnel program it started as, (the included tunnel proggie) It will now take a argument and dig to that distance, bouncing back and forth to empty out, I'm currently using wooden piping to empty it out and then sorting afterwards, it can tell when it's full and empty (ish) and all that works pretty well, and it's not too slow either. But it never quite does the distance it's supposed to, for instance i tell it to go 200 blocks it'll do 250?! I can't see in my 9rather messy code0 where the problem is, so I was hoping that someone here could help me, this was written on the 1.33 version of the mod, updating today I hope. Please, any help would be great and bear in mind I'm a Lua noob lol.



local depth = 0
local collected = 0
local dtravel = 0
local b = 1
local totalTrav = 0
local tArgs = { ... }
local toTravel = 0

if #tArgs < 1 then
    print("iBranch v1.0.0.1a")
print("-----------------")
print( "Usage: ibranch <length> [skip]" )
return
end


if #tArgs == 2 then
toTravel = tonumber( tArgs[2] )
end

local length = tonumber( tArgs[1] )
if length < 1 then
print( "Tunnel length must be positive" )
return
end



local function collect()
collected = collected + 1
if math.fmod(collected, 25) == 0 then
print( "Mined "..collected.." blocks." )
end
end

local function checkInv()
for n=1,9 do
if turtle.getItemCount(n) == 0 then
return true
end
end
return false
end

function checkEmpty()
if (turtle.getItemCount(1) == 0) and (turtle.getItemCount(2) == 0) and (turtle.getItemCount(3) == 0) and (turtle.getItemCount(4) == 0) and (turtle.getItemCount(5) == 0) and (turtle.getItemCount(6) == 0) and (turtle.getItemCount(7) == 0) and (turtle.getItemCount(8) == 0) and (turtle.getItemCount(9) == 0) then
return true;
end
end

local function tryDig()
while turtle.dig() do
collect()
sleep(0.5)
if not turtle.detect() then
return true
end
end
return not turtle.detect()
end

local function tryDigUp()
while turtle.digUp() do
collect()
sleep(0.5)
if not turtle.detectUp() then
return true
end
end
return not turtle.detectUp()
end

local function traverse(trav)
    print( "Travelling... " .. trav .. " blocks forward");
for b=1,trav do
turtle.forward()
depth = depth + 1
end
return depth;
end


function doWall2()
tryDigUp()
turtle.up();
tryDigUp()
turtle.up();
turtle.turnLeft();
tryDig()
turtle.turnRight();
turtle.turnRight();
tryDig();
turtle.down();
tryDig();
turtle.turnLeft();
turtle.turnLeft();
tryDig();
turtle.down();
tryDig();
turtle.turnRight();
turtle.turnRight();
tryDig();
turtle.turnLeft();
tryDig();
end

function doTunnel()
for n=1,length do
turtle.placeDown()
doWall2();
if n<length then
tryDig()
if not turtle.forward() then
print( "Aborting Tunnel." )
break
else
depth = depth + 1
end
else
print( "Tunnel complete." )
-- depth = depth + 1
break
end
if checkInv() then
-- Inventory empty still
else
-- No new space in Inventory
print("Returning to origin, inventory full")
break
end;
if n >= length then
break;
end;
end

print( "Returning to start..." )

-- Return to where we started
turtle.turnLeft()
turtle.turnLeft()
dtravel = depth
while depth > 0 do
if turtle.forward() then
depth = depth - 1
else
turtle.dig()
end
end
turtle.turnRight()
turtle.turnRight()
totalTrav = dtravel;
end


-- Main Routine

print("iBranch v2.0.0.0B")
print("-----------------")
print( "Branch Mining... " .. tArgs[1] .. " blocks along.")

if toTravel>0 then
    depth = traverse(tonumber(tArgs[2]))
end

while totalTrav < length do
doTunnel();
if totalTrav < length then
-- Wait for inventory to empty, then go again.
left = length - totalTrav;
toTravel = totalTrav;
print("Length = " .. length .. " totalTrav = " .. totalTrav ..  " skip = " .. totalTrav .. " left = " .. left);
while not checkEmpty() do
sleep(5);
end;
traverse(toTravel);
end;
end; 

print( "Tunnel complete." )
print( "Mined "..collected.." blocks total." )
print( "Traversed "..dtravel.." blocks along.")

Pastebin: http://pastebin.com/LKtrvDGE
Bonum #2
Posted 29 July 2012 - 02:16 AM
I can't really see an error, but… You have so many variables. depth, length, toTravel, totalTrav, dTravel. You also modify the depth variable in four different places. Maybe if you rewrite and clean up some, you can solve it.

Try and make a wrapper function to all movement, and make sure to only use that function to move. And only change depth in that function.

Another suggestion is to try the gps function. :ph34r:/>/>