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

BranchMiner

Started by CoderJohn, 14 November 2012 - 01:12 PM
CoderJohn #1
Posted 14 November 2012 - 02:12 PM
This is a branch miner program… to use it add it to your programs in a miner turtle as miner.
Then start it using these arguments:
miner [total branches] [blocks between branches] [length of branch]

Pastebin: http://pastebin.com/hC4g99bj


local prgm_args = {...};
if(prgm_args[1] == 'usage' or prgm_args[1] == '?' or prgm_args[1] == 'help')then
  print("miner <branches> <blocks between branches> <branch length>");
  return true;
end

local branches = tonumber(prgm_args[1]) or 5;
local branch_interval = (prgm_args[2]~=nil and tonumber(prgm_args[2])+1) or 4;
local branch_length = tonumber(prgm_args[3]) or 10;
local trunk_length = branches*branch_interval;

local cTrunk
local cBranch = 0;
function dig(length)
  local cLength = 0;
  local bDetect, bMoved;
  while(cLength < length)do
    repeat turtle.dig(); os.sleep(0.1); bDetect = turtle.detect(); until bDetect == false;
    bMoved = turtle.forward();
    if(bMoved)then
      cLength = cLength + 1;
      turtle.digUp();
      turtle.digDown();
    end
  end
  while(cLength~=0)do
    bMoved = turtle.back();
    if(bMoved)then
      cLength = cLength - 1;
    end
  end
end

function digTrunk()
  local cLength = 0;
  local bDetect, bMoved;
  while(cLength < trunk_length)do
    repeat turtle.dig(); os.sleep(0.1); bDetect = turtle.detect(); until bDetect == false;
    bMoved = turtle.forward();
    if(bMoved)then
      cLength = cLength + 1;
      turtle.digUp();
      turtle.digDown();
      if(cLength%branch_interval==0)then
        turtle.turnRight();
        dig(branch_length);
        turtle.turnLeft();
        turtle.turnLeft();
        dig(branch_length);
        turtle.turnRight();
      end
    end
  end
  while(cLength~=0)do
    bMoved = turtle.back();
    if(bMoved)then
      cLength = cLength - 1;
    end
  end
end

digTrunk();
trondaron #2
Posted 15 November 2012 - 09:42 AM
Just want to check I'm understanding correctly. This program will just mine the tunnel/branches, not the ore veins it finds in them.
Correct?
CoderJohn #3
Posted 15 November 2012 - 03:06 PM
yes, as turtles can't detect ores…. but yes… it mines a tunnel and the branches.
billysback #4
Posted 15 November 2012 - 08:32 PM
They can, can't they? if you give them an ore in each of their slots (obviously you couldn't do redstone, lapiz and diamond) then you can just do

turtle.select(slot)
if turtle.compare() then turtle.dig() end
CoderJohn #5
Posted 16 November 2012 - 02:14 AM
Well yeah, but then your wasting slots… and your doing a bunch of extra calculations that will inevitably slow down the mining of the turtle.
CoderJohn #6
Posted 16 November 2012 - 03:22 PM
I updated the program… when it finishes the mine it now comes back to you.. Feel free to edit my work..
ChunLing #7
Posted 18 November 2012 - 10:54 PM
Use reverse comparison, give the turtle a sample of stone, dirt, and gravel (not wasting space because it'll run into plenty of those anyway) and as it mines have it compare around and find (and dig out) things that aren't any of those three.
Viproz #8
Posted 19 November 2012 - 12:11 AM
I am doing a turtle who detect minerals veins you can find it on the ask a pro section, it's not finished yet but it's works !
CoderJohn #9
Posted 21 November 2012 - 09:33 AM
I could do that… Pretty simple :(/>/>
backwoods #10
Posted 22 November 2012 - 05:13 AM
I dont understand it everytime I use a custom script like yours here the turtle will mine 1 block and completly freeze up.. I have to break it to be able to type anything in it again.. sometimes it doesnt even mine 1 block. Any ideas why this would be happening? I Put the file into the CC rar into the programs/turlte directory..

This is driving me nutz.. I have fuel in it….

The regular old default tunnel program works..

EDIT: I set the Need fuel option to False in the config and it might be workign now.. I dont know I got to watch it to see if it stops again.. I dont get it I have coal in the darn thing before..

EDIT: well I don't know if lava is suppoed to stop turtles but it hit a lava pool and frooze.. It only dug the mine branch and was just starting the first real branch when it frooze.
kazagistar #11
Posted 28 November 2012 - 10:32 AM
I dont understand it everytime I use a custom script like yours here the turtle will mine 1 block and completly freeze up.. I have to break it to be able to type anything in it again.. sometimes it doesnt even mine 1 block. Any ideas why this would be happening? I Put the file into the CC rar into the programs/turlte directory..

This is driving me nutz.. I have fuel in it….

The regular old default tunnel program works..

EDIT: I set the Need fuel option to False in the config and it might be workign now.. I dont know I got to watch it to see if it stops again.. I dont get it I have coal in the darn thing before..
Is the fuel in the inventory, or did you actually refuel? You need to actually actually call the refuel command.
Ulthean #12
Posted 04 December 2012 - 03:46 AM
I have made three programs that do the following tasks respectively:
  1. Dig the central hallway (not needed, but I like big corridors)
  2. Dig 2 high branches on either side of the central hallway
  3. Excavate any ores it encounters in the branches
    1. For this I used the same technique ChunLing mentioned, though I use 6 slots to be totally safe (strongholds, mineshafts, bedrock)
    2. –> Dirt, stone, marble, stone bricks, wood, bedrock (gravel isn't that big of a problem, and it saves an inventory slot)
    3. After each branch it returns to its docking station, unloads, refuels and moves on to the next branch
I chose to split phase 2 and 3 (the digging of the shafts and the excavation respectively) in order to have as much inventory space as possible for the excavation.
Before I did this cobblestone kept filling up my inventory.

If somebody is interested I can put the code here (still need some cleanup here and there) but it is pretty awesome.
ChunLing #13
Posted 04 December 2012 - 10:15 AM
You want to order your blocks by frequency for best running efficiency. Stone,Gravel (cause yes, it is a bit problem if you're not wanting to collect it),Dirt,etc. (I wouldn't bother avoiding wood, you can just burn it as fuel if you don't want it).
Ulthean #14
Posted 04 December 2012 - 11:52 AM
I ordered them by frequency, and I say that gravel isn't that big a problem since the 'veins' are usually pretty small.Though now I realize this is the same with wood.
Ulthean #15
Posted 04 December 2012 - 11:54 AM
This is the code I have for the excavation program atm, I haven't tested it since cleanup, since my turtles are still excavating using the previous version atm.

I am new to the forums, so I am not familiar with all the programs on here, but I am pretty fond of the simple 'copy/paste' functions I have written below, they are
so much easier to use than the basic API


-- DEFAULT CONFIG OF THIS PROGRAM, THOUGH COMMAND LINE PARAMETERS CAN BE USED
-- FOR THE DIRECTION PARAMETERS USE THE VAlUES DECLARED IN THE COPY/PASTED CODE BELOW
args={...}
numShafts = args[1] or 16
blocksBetweenShafts = args[2] or 3
firstShaftOffset = args[3] or 4
centerRadius = args[4] or 4
numComponents = args[5] or 6
fuelSuckDir = args[6] or 3
fuelDropDir = args[7] or 4
itemDropDir = args[8] or 4
-- START OF COPY/PASTED CODE EXTENDING THE FUNCTIONALITY OF THE BASIC API
-- SOME OF THESE FUNCTIONS MIGHT BE REDUNDANT FOR THIS PROGRAM
forward = 1
back  = 2
up = 3
down = 4
right = 5
left = 6
function turnTo(dir)
  if dir==back then
	turtle.turnLeft()
	turtle.turnLeft()
  elseif dir==right then
	turtle.turnRight()
  elseif dir==left then
	turtle.turnLeft()
  end
end
function turnFrom(dir)
  if dir==back then
	turtle.turnLeft()
	turtle.turnLeft()
  elseif dir==right then
	turtle.turnLeft()
  elseif dir==left then
	turtle.turnRight()
  end
end
function turnedDir(dir)
  if dir==right or dir==left or dir==back then
	return forward
  else
	return dir
  end
end
function reverseDir(dir)
  if dir==forward then
	return back
  elseif dir==back then
	return forward
  elseif dir==up then
	return down
  elseif dir==down then
	return up
  elseif dir==right then
	return left
  elseif dir==left then
	return right
  end
end
function move(dir, steps)
  steps = steps or 1
  turnTo(dir)
  local tDir=turnedDir(dir)
  for i=1,steps,1 do
	success=false
	while not success do
	  if tDir==forward then
		success=turtle.forward()
	  elseif tDir==up then
		success=turtle.up()
	  elseif tDir==down then
		success=turtle.down()
	  end
	end
  end
  turnFrom(dir)
  return true
end
function dig(dir)
  turnTo(dir)
  local tDir=turnedDir(dir)
  while detect(tDir) do
	if tDir==forward then
	  turtle.dig()
	elseif tDir==up then
	  turtle.digUp()
	elseif tDir==down then
	  turtle.digDown()
	end
  end
  turnFrom(dir)
end
function digAndMove(dir, steps)
  steps = steps or 1
  turnTo(dir)
  local tDir=turnedDir(dir)
  for i=1,steps,1 do
	while detect(tDir) do
	  dig(tDir)
	  sleep(0.25)
	end
	move(tDir)
  end
  turnFrom(dir)
end
function place(dir, slot)
  turtle.select(slot)
  turnTo(dir)
  local tDir=turnedDir(dir)
  if tDir==forward then
	turtle.place()
  elseif tDir==up then
	turtle.placeUp()
  elseif tDir==down then
	turtle.placeDown()
  end
  turnFrom(dir)
end
function detect(dir)
  block = false
  turnTo(dir)
  local tDir=turnedDir(dir)
  if tDir==forward then
	block = turtle.detect()
  elseif tDir==up then
	block = turtle.detectUp()
  elseif tDir==down then
	block = turtle.detectDown()
  end
  turnFrom(dir)
  return block
end
function compare(dir, slot)
  same = false
  turtle.select(slot)
  turnTo(dir)
  local tDir=turnedDir(dir)
  if tDir==forward then
	same = turtle.compare()
  elseif tDir==up then
	same = turtle.compareUp()
  elseif tDir==down then
	same = turtle.compareDown()
  end
  turnFrom(dir)
  return same
end
function compareAndReplace(dir, compareSlot, replaceSlot)
  replaced = false
  turnTo(dir)
  local tDir=turnedDir(dir)
  if not compare(tDir, compareSlot) then
	replaced = true
	dig(tDir)
	place(tDir, replaceSlot)
  end
  turnFrom(dir)
  return replaced
end
function drop(dir, num)
  turnTo(dir)
  local tDir=turnedDir(dir)
  if tDir==forward then
	turtle.drop(num)
  elseif tDir==up then
	turtle.dropUp(num)
  elseif tDir==down then
	turtle.dropDown(num)
  end
  turnFrom(dir)
end
function suck(dir)
  turnTo(dir)
  local tDir=turnedDir(dir)
  if tDir==forward then
	turtle.suck()
  elseif tDir==up then
	turtle.suckUp()
  elseif tDir==down then
	turtle.suckDown()
  end
  turnFrom(dir)
end
function refuel(suckDir, dropDir, fuelSlot, requiredFuel)
  turtle.select(fuelSlot)
  while turtle.getFuelLevel() < requiredFuel do
	suck(up)
	turtle.refuel()
	drop(down)
  end
end
-- END OF COPY/PASTED CODE EXTENDING THE FUNCTIONALITY OF THE BASIC API
local excavate
local check
function moveToShaft(num, interval, offset)
  digAndMove(forward, offset+((interval+1)*(num-1)))
end
function returnFromShaft(num, interval, offset)
  digAndMove(forward, offset+((interval+1)*(num-1)))
  turnTo(back)
end
function dropoff(dir)
  for i=1,numComp do
	turtle.select(i)
	drop(down, turtle.getItemCount(i)-1)
  end
  for i=numComp+1,16 do
	turtle.select(i)
	drop(down)
  end
end
function moveToNextTunnel(radius, fuelSuckDir, fuelDropDir)
  refuel(fuelSuckDir, fuelDropDir, 1, 2*radius)
  turtle.turnLeft()
  if radius>0 then
	digAndMove(forward, radius)
	turtle.turnLeft()
	digAndMove(forward, radius)
	turtle.turnRight()
  end
end
function isSpecial(dir, components)
  turnTo(dir)
  local tDir=turnedDir(dir)
  for i=1, components do
	if compare(tDir, i) then
	  turnFrom(dir)
	  return false
	end
  end
  turnFrom(dir)
  return true
end
function check(dir, components)
  turnTo(dir)
  local tDir=turnedDir(dir)
  if detect(tDir) then
	if isSpecial(tDir, components) then
	  digAndMove(tDir)
	  excavate(components)
	  digAndMove(reverseDir(tDir))
	end
  end
  turnFrom(dir)
end
function checkAround(components)
  for i=1,4 do
	turtle.turnLeft()
	check(forward, components)
  end
end
function excavate(components)
  check(up, components)
  check(down, components)
  checkAround(components)
end
function checkSides(vDir, components)
  check(vDir, components)
  check(left, components)
  check(right, components)
end
function excavateShaft(dir, num, interval, offset, radius, components)
  turnTo(dir)
  for i=1,(radius+(offset+((interval+1)*(num-1)))) do
	digAndMove(forward)
	checkSides(up, components)
  end
  digAndMove(down)
  turnTo(back)
  for i=1,(radius+(offset+((interval+1)*(num-1)))) do
	digAndMove(forward)
	checkSides(down, components)
  end
  turnFrom(dir)
  digAndMove(up)
end
function excavateShafts(num, interval, offset, radius, components, fuelSuckDir, fuelDropDir, itemDropDir)
  for i=1, num do
	refuel(fuelSuckDir, fuelDropDir, (100+(2*(offset+((interval+1)*(num-1))))+(4*(radius+(offset+((interval+1)*(num-1)))))))
	moveToShaft(i, interval, offset)
	excavateShaft(left, i, interval, offset, radius, components)
	returnFromShaft(i, interval, offset)
	dropoff(itemDropDir)
	refuel(fuelSuckDir, fuelDropDir, (100+(2*(offset+((interval+1)*(num-1))))+(4*(radius+(offset+((interval+1)*(num-1)))))))
	moveToShaft(i, interval, offset)
	excavateShaft(right, i, interval, offset, radius, components)
	returnFromShaft(i, interval, offset)
	dropoff(itemDropDir)
  end
end
function excavateAllShafts(num, interval, offset, radius, components, fuelSuckDir, fuelDropDir, itemDropDir)
  for i=1,4 do
	excavateShafts(num, interval, offset, radius, components, fuelSuckDir, fuelDropDir, itemDropDir)
	moveToNextTunnel(radius, fuelSuckDir, fuelDropDir)
  end
end
excavateAllShafts(numShafts, blocksBetweenShafts, firstShaftOffset, centerRadius, numComponents, fuelSuckDir, fuelDropDir, itemDropDir)
bwcbwc #16
Posted 26 February 2013 - 12:08 PM
Rather than fully automating the turtle, I like to let it dig the tunnels and then follow along with a Fortune II+ pick (unbreaking or repair is nice too) to dig out the ores like diamond, ruby and redstone. I have a similar branchmine program to the OP, except it always tunnels every 3rd row of blocks instead of accepting a parm. I also borrowed heavily from the built-in turtle tunnel program from CC for refuel logic and parm checking.
http://pastebin.com/XdRxwvkg

usage:

branchmine (length of tunnel) (# of tunnels)
turtle will dig tunnels 3 high and 1 wide in a zig-zag pattern moving to the left, and return to original location, digging across one side of the zig-zag. So from the turtle's original position, there will be a cross tunnel to the left and then a series of U-shaped pairs of tunnels of the specified length. Move the turtle forward to the end of the first tunnel and repeat. So if you specify "branchmine 16 6" it will dig a zig-zag of 6 tunnels, each of length 16. Since the tunnels are 3 blocks apart, this is very close to branchmining exactly one chunk.