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

Branch Mining Turtle [Torches, 3x3 high] v0.1

Started by Noiro, 22 June 2013 - 06:41 AM
Noiro #1
Posted 22 June 2013 - 08:41 AM
Hello everyone! This is one of the first programs I think is good enough for the public. While I'm sure there are plenty of these, I prefer to make my own tools (for the experience), and I figure some would like said tools as well. So, please note that there are a few things on this program I have not fixed, but it works for the most part (Gravel occasionally being in tunnel he finishes, torch lighting not being perfect, etc.)

Features
  • Places torches every 10 blocks
  • Places blocks on top and bottom center tunnel if none are present
  • Tunnel is 3x3 instead of 3x2
  • Calculates how many cycles he can perform based on how many resources he has (no dead turtles)
  • Places items in chest at end of branch (2 opposite branches share a chest)
  • Handles Gravel, Sand, and monsters standing in front of him
Future Features
  • Configurable Cycles as to how many you want
  • Fix for occasionally having gravel/sand in branches
  • Make turtle return to starting position once finished
  • More fuel/using mode which will check tunnels and completely fill edges to ensure monsters can't drop in from caves
Uses
  • Have AE? Mine only Quartz levels!
  • Diamond level-only mining!
  • Building long, large mineshafts stretching for AGES.
My only big suggestion would be to make the turtle's startup program (or just adding it in) launch: rednet.open("side") on the side of a Chunk Loader upgrade if you have Misc Periphs as this guy WILL cover a large area and leaving him to do his thing for a while is suggested.

How to use: Put Chests in slot 2, and torches in slots 3 and 4 and start program.
The first thing he mines should be stone if possible. One cobble in slot 1 would work additionally if it will not be.
So, without further ado, here is v0.1 of the progam! Ideas, improvements critiques, etc are all welcomed as well.

Code:
Spoiler

	--Branch Mining Program--
	
	torchDist = 10
	
	--Start of Torch stuff
	function placeTorch()
	  print("Placing torch...")
	  if turtle.getItemCount(3) == 1 then
		turtle.select(4) --Considering adding a method of returning home if no torches present in second slot
			turtle.place()
	  else
	   turtle.select(3)
			  turtle.place()
	   turtle.select(1)
	  end
	  torchDist = 1
	end
	
	function needTorch()
	  if torchDist == 10 then
		placeTorch()
	  end
	end
	--End of Torch stuff
	
	function forward()
	  while not turtle.forward() do
		turtle.dig()
			sleep(.1)
	  end
	end
	
	function dig()
	  while turtle.detect() do
		turtle.dig()
			sleep(.8)
	  end
	end
	
	function digUp()
	  while turtle.detectUp() do
		turtle.digUp()
			sleep(.8)
	  end
	end
	
	function clear()
	  shell.run("clear")
	end
	
	function calculate()  --This essentially inspects resources in slots and figures out how much he CAN do. He won't ever get stuck because he makes sure he has fuel first.
	  local fuel = turtle.getFuelLevel()
	  local chests = turtle.getItemCount(2)
	  local torches = turtle.getItemCount(3)+turtle.getItemCount(4)
	  local fuelCycles = math.floor(fuel/500)
	  local torchCycles = math.floor(torches/4)
	  local chestCycles = math.floor(chests*2)
	  local cycles = 0
	
	  if fuelCycles<torchCycles then   --This big block takes cycles and figures out which between chests, fuel, and torches can perform
		if fuelCycles<chestCycles then --least amount of cycles and sets cycles to that amount. Ensures resources for every cycle.
			   cycles = fuelCycles
			else
			   cycles = chestCycles
			end
	  elseif torchCycles<fuelCycles then
		if torchCycles<chestCycles then
			   cycles = torchCycles
			else
			   cycles = chestCycles
			end
	  elseif fuelCycles<chestCycles then
		if fuelCycles<torchCycles then
			   cycles = fuelCycles
			else
			   cycles = torchCycles
			end
	  elseif chestCycles<fuelCycles then
		if chestCycles< torchCycles then
			   cycles = chestCycles
			else
			   cycles = torchCycles
			end
	  elseif torchCycles<chestCycles then
		if torchCycles< fuelCycles then
			   cycles = torchCycles
			else
			   cycles = fuelCycles
			end
	  elseif chestCycles<torchCycles then
		if chestCycles< fuelCycles then
			   cycles = chestCycles
			else
			   cycles = fuelCycles
			end
	  else
		cycles = fuelCycles
	  end
	
	  if fuel <500 or torches<10 then
		print("I need both 500 fuel and at least 10 torches.")
			print("I have "..torches.." torches and "..fuel.." fuel.")
	  else
	 clear()
			print("-----------------------------------")
			print("Fuel is: "..fuel)
			print(chests.." chests in slot 2")
			print(torches.." torches in slots 3 and 4")
			print("Doing "..cycles.." cycles with these resources.")
			print("-----------------------------------")
			print("Is this acceptable? (y/n)")
			local input = "{"
			while not input == "y" or "n" do
			  input = read()
			  if input == "y" then
				branchControl(cycles)
			  elseif input == "n" then
				print("Shutting down program...")
					sleep(1.5)
					clear()
					break
			  else
				print("That is not a valid option! Please type y or n and hit ENTER")
			  end
			end
		  
		  
	  end
	end
	
	function chestDrop()
	  if turtle.detectDown() then
		local x = 5
			while x<17 do
			  turtle.select(x)
			  turtle.dropDown()
			  x=x+1
			end
	  else
	  local x=5
		turtle.select(2)
			turtle.placeDown()
			while x<17 do
			  turtle.select(x)
			  turtle.dropDown()
			  x=x+1
			end
	  end
	end
	
	function branchControl(cycles)
	 while cycles>0 do
	 if cycles>0 then
	  tunnel(5, false)
	  turtle.turnRight()
	  forward()
	  tunnel(30, true)
	  turtle.up()
	  forward()
	  chestDrop()
	  forward()
	  turtle.down()
	  cycles = cycles -1
	 else
	   break
	 end
	 if cycles>0 then
	  tunnel(30, true)
	  turtle.up()
	  forward()
	  chestDrop()
	  turtle.turnRight()
	  forward()
	  turtle.turnLeft()
	  dig()
	  digUp()
	  turtle.up()
	  dig()
	  turtle.turnRight()
	  turtle.turnRight()
	  dig()
	  turtle.down()
	  dig()
	  turtle.digDown()
	  turtle.down()
	  if turtle.detectDown() then
		turtle.select(1)
		turtle.placeDown()
	  end
	  dig()
	  turtle.turnLeft()
	  turtle.turnLeft()
	  dig()
	  turtle.turnRight()
	  cycles = cycles-1
	  else
		break
	  end
	 end
	 break
	end
	
	function tunnel(distance, isReturn) -- This is the main loop where he digs the tunnel. If isReturn true, he will return to the front of the tunnel once he's done. If false, he'll stay put.
	  local x =0
	  while x<distance do
		forward()
			if not turtle.detectDown() then
			  turtle.select(1)
			  turtle.placeDown()
			end
			turtle.turnLeft()
			dig()
			needTorch()
			digUp()
			turtle.up()
			dig()
			digUp()
			turtle.up()
			if not turtle.detectUp() then  --Sort of helps cover the place? Top and bottom at least get inspection XD
			  turtle.select(1)
			  turtle.placeUp()
			end
			dig()
			turtle.turnRight()
			turtle.turnRight()
			dig()
			turtle.down()
			dig()
			turtle.down()
			dig()
			turtle.turnLeft()
			x=x+1
			torchDist = torchDist+1
	  end
	
	  --And here begins the returning home process
	  turtle.turnLeft()
	  turtle.turnLeft()
	  if isReturn == true then
		local x=0
		while x<distance do
		  forward()
			  x=x+1
		end
	  end
	end
	
	
	function init()
	  calculate()
	end
	
	init()


And here is a convenient Pastebin for those with HTTP:
http://pastebin.com/92fW3KX2
Noiro #2
Posted 30 June 2013 - 01:45 PM
From the more experienced coders, I can haz feedback?
Mackan90096 #3
Posted 04 July 2013 - 12:13 PM
You has error

line 201: no loop to break