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:
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.
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:
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: