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

Turtle mining program, for your review.

Started by Wutdaflux, 13 January 2013 - 08:33 AM
Wutdaflux #1
Posted 13 January 2013 - 09:33 AM
Howdy all,

I'm fairly new to programming, but have always been enamored by it. When I found out about computercraft, I jumped at the chance to try it.

I'm sure you guys have seen hundreds of turtle mining programs, but I wanted to put mine up for comparison, and maybe help out others.

Anyway, let me give the setup of how this program works.

1. I set up a small base, usually cobblestone, about 20 or 30 blocks above the ground, no staircase (have a jetpack) in front of a generally open, semi flat area with no wall on the side I plan on mining. I do this so I can concentrate on the turtles and not worry about mobs.

2. Near the front-center I place disk drive and a wooden chest with one space between them (drive on left, chest on right).

3. You'll need a disk. That disk will contain the program below and another file named "startup" with this in it:

os.run({}, "disk/automine")  --automine is the program name

4. Within the chest, using only the first four slots, put a stack of sand, dirt, gravel, and cobblestone. At the beginning of the program the turtle will turn and take the stacks, then put back all but one back in the chest. This will be used later to dump unwanted materials, maximizing the space available for stuff you actually want.

5. Figure out the average start height that the turtle will start digging. You use that to set up how deep the turtle will go before he comes back. The default is 60, but you can change that and it will carry over as long as you have your disk in the disk drive.

I hope that was clear enough, so after all that, here's the code.



-- Set Variables
local d
local z = 0
local s = 1
local x = 0
local o = 0
local a = 0
local r = 0
local out = 0
local across
local dir = ""
local c = "N"
local e

-- Define Functions

function fcheck()  -- ### Checks for depth file

   if fs.exists("disk/depth") then  
	  h = fs.open("disk/depth", "r")
	  d = tonumber(h.readAll())
   else
	  h = fs.open("disk/depth", "w")
	  h.write(tonumber(60))
	  d = tonumber(60)
   end
end

function facing()
   e = tonumber(d % 2)
end


--  ### determine direction

function getout() -- ### leave homebase
   turtle.select(1)
   turtle.turnRight()
   rednet.send(506, " Gathering Supplies.")
   turtle.suck(1)
   turtle.suck(2)
   turtle.suck(3)
   turtle.suck(4)
   cycle()
   turtle.turnLeft()
   turtle.forward()
   turtle.forward()
   turtle.forward()
   turtle.forward()
end


function goout()  -- ### move to startpoint
   if out == 0 then
	  sleep(0.1)
   else
	  repeat
		 turtle.forward()
		 o = o + 1
	  until o >= out  
   end
end


function right()  --  ### direction if right
   turtle.turnRight()
	  repeat
		 turtle.forward()
		 a = a + 1
	  until a == across
   turtle.turnLeft()
end

function left()  -- ### direction if left
   turtle.turnLeft()
	  repeat
		 turtle.forward()
		 a = a + 1
	  until a == across  
   turtle.turnRight()
end

function godown()  -- ### stop when on ground
repeat
turtle.down()
r = r + 1
   until turtle.detectDown() == true

end

function drop(slot)  -- ### Gets item counts
   turtle.select(slot)
   turtle.drop(turtle.getItemCount(slot) -1)
end

function cycle()  -- ### Drops unwanted items
s = 1
repeat
drop(s)
s = s + 1
until s == 5
   turtle.select(1)
end

function dig() -- ### Digging function 3x3
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.turnLeft()
if turtle.detect() == true then
  repeat
	 turtle.dig()
	 sleep(1)
  until turtle.detect() == false
end
turtle.attack()
turtle.attack()
turtle.forward()
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.turnLeft()
if turtle.detect() == true then
  repeat
	 turtle.dig()
	 sleep(1)
  until turtle.detect() == false
end
turtle.attack()
turtle.attack()
turtle.forward()
turtle.turnLeft()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.turnRight()
end

function down() -- ### turtle digs down after finishing row
   rednet.send(506, " Rows dug: "..z)
   turtle.digDown()
   turtle.down()
   z = z + 1
end

function comeback() -- ### returns turtle to base

facing()

if e == 0 then
	  turtle.turnRight()
	  turtle.turnRight()
end
	  repeat
	  turtle.up()
	  r = r - 1
   until r == 0

   if dir == "r" then
	  turtle.turnRight()
   elseif dir == "l" then
	  turtle.turnLeft()
   end

   repeat
	  turtle.forward()
	  a = a - 1
   until a == 0

   if dir == "r" then
	  turtle.turnLeft()
   elseif dir == "l" then
	  turtle.turnRight()
   end

   repeat
	  turtle.forward()
	  o = o - 1
   until o <= -4
end

function update()
h = fs.open("disk/depth", "w")
print("What would you like to change the depth to?:  ")
   d = tonumber(read())
	  h.write(d)
	  h.close()
print("Depth changed to "..d..".  Restarting program.")
os.reboot()
end

-- ### Program Start

fcheck()

rednet.open("right")  -- ### send message to tracking monitor
rednet.send(506, " Requesting input.")

print("--> Your current depth is set to "..d..". Type '999' to update number.\n")

print("How far out?  ") -- ### how far out to do want to go?
   out = tonumber(read())
	  if out == 999 then
		 update()
	  end

repeat
print("'r', 'l', or 's'(straight)?  ") -- ### left or right
   dir = read()
	  if (dir == "r") or (dir == "l") then
		 print("How far across?"  )  -- ### distance from center.
			across = tonumber(read())
	  elseif dir == ("s") then
		 across = tonumber(0)
	  else
		 print("Invalid Entry.")
	  end
until (dir == "l") or (dir == "r") or (dir == "s")

getout()

goout()

if dir == ("r") then
   right()
elseif dir == ("l") then
   left()
else
   sleep(0.1)
end

godown()	

rednet.send(506, " Starting.")

turtle.digDown()
turtle.down()

repeat
dig()
down()
x = x + 1
if x == 6 then
  cycle()
  x = 1
end
until z == d

repeat
turtle.up()
z = z - 1
until z == 0

rednet.send(506, " Heading back")
comeback()

Constructive criticism is welcome. I'm new to this, so I'm sure there's better ways to do what I've done.

Oh, and ignore the rednet commands. That's optional.

Looking forward to your responses.

Flux.

EDIT:
xInDiGo #2
Posted 13 January 2013 - 11:41 AM
if i wanted to use this in the nether, say, to unbury a nether fortress. could i use nether brick/stair/fence as the filter and it'll just ignore those bricks?


::edit::

i see now that it drops unwanted items, instead of ignoring them.
Wutdaflux #3
Posted 13 January 2013 - 01:33 PM
Yea, I've read alot about people wanting a turtle.detectid so the turtle will ignore unwanted chunks, but it's probably not going to happen. Instead, the turtle goes down far enough to never get more than 64 of the unwanted materials, and then it dumps all but one of them. This helps keep the turtle's inventory free for more important things at the expense of four slots. Beats the alternative which is a turtle that fills up way too damn fast with cobblestone. :)/>

Oh, and to answer your question, you can put anything you don't want in those four slots. I'm working on making an option to change the number of slots. :)/>
Ninja_Ducky #4
Posted 13 January 2013 - 03:21 PM
An interesting program. I'd like to have a go at it, but it's a bit tricky to just copy/paste code because of the way the formatting gets off. If you create a profile and upload the code to pastebin.com we can download the program onto a computer from within minecraft using
rom\programs\http\pastebin get <pastebin identifier> <name we want to label it as>
This requires your server have the http API enabled. Also, there have been some programs that use "noise blocks" to ignore certain types of blocks (dirt, sand, etc.) by comparing them to items already in the turtle's inventory. AustinKK wrote a pretty awesome quarry program that does that to only mine out ore and, optionally, empty chests the turtle encounters. The link is http://www.computerc...tle-ore-quarry/ if you want to have a look. Great work though, I'm just getting back into coding myself and would love to give yours a whirl.

Edit:
I forgot to mention that if you're using the Railcraft mod on your server there is a device that allows chunks to stay loaded if you're not in the area. You'd mentioned it in your video and I'm not sure what mods/mod pack you're running. Feed the Beast is my server's current preference and includes several really fun mods to include computercraft and railcraft.

World Anchor:
http://railcraft.wik...or+%28Device%29