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

[CC 1.4] Lettuce's Advanced Mining Programs!

Started by Lettuce, 24 August 2012 - 06:38 PM
Lettuce #1
Posted 24 August 2012 - 08:38 PM
*edit: Now with pictures!

This is my latest project (projects?). I wanted to utilize dan200's recent additions to ComputerCraft, most notably the turtles' ability to place items in chests. The program pack comes with three modules, and two flavors of mineshaft. The "mine" program is the best I've ever made. You choose the x,y, AND z values to give greater flexibility/freedom. The turtle places torches in all three programs to keep the mines clear of zombies and skeletons and *BLAM(s)*
A brief explanation of all is available, as well as instructions, and most importantly: CODES!

What does it do?

Spoiler"vertshaft"
–Digs straight down, placing ladders, cobble and torches as necessary on the way back up.
[attachment=407:vertshaft.JPG]
Descending a vertshaft ^ ^ ^
"stairshaft"
–Makes the familiar staircase-shaft we all know and love. Lights the shaft as it goes, and mines an extra head-room block so players can add stairs/rails.
[attachment=408:stairshaft.JPG]
A stairshaft, standing next to a torch.
"mine"
–The real reason I posted this. You set the width, then the length, and lastly, height. The turtle drops the items in a chest (or the ground, if you don't provide one) and returns to mining. It lights the mine as it goes.
[attachment=409:mine.JPG]
An 11 * 10 * 5 mine, depth of 20 blocks. Complete with 1 torch.
These programs tell you what to give it and where to put them.

How do I use it?

SpoilerThe more important question is: where do you put it? The program is pretty simple to grasp, and once you use it, you will love it.

"vertshaft"
–The program will ask you how deep you want to make your mine. Type in a positive number, and provide ladders, torches, coal, and cobble where it tells you to put them. Then press any character key, like "q."
"stairshaft"
–Again, it asks you how deep you want the mine, and requests coal, torches, and cobble in their respective slots. Then you press any character key, like "q."
"mine"
–This requires a little more explanation. Watch the turtle until it completes the second row. Once it does, look at the way it mined. The turtle probably mined a few to many blocks in the first row, but it corrects itself on the second row. On the same wall as the entrance, place a chest in the wall corner and mirror on the other side.
(see mine.jpg for visual aid, under "What does it do?")
The turtle does place torches as it goes, but it doesn't start doing that for five rows, which can take a while. I recommend lighting it yourself at this stage, and that's it. You'll be raking in the diamonds. Or build yourself a castle with the massive, massive amount of cobble you will amass.

Most importantly, the CODE:

Spoiler"vertshaft"
Spoilerglow = 0
function moveForward()
repeat
turtle.dig()
until turtle.forward()
end

function mineUp()
repeat
turtle.digUp()
until turtle.up()
end

function refuel()
if turtle.getFuelLevel() <= 5 then
turtle.select(16)
turtle.refuel(1)
end
end

function selectLadder()
turtle.select(15)
if turtle.getItemCount(15) == 1 then
turtle.select(1)
while not turtle.compareTo(15) do
for i = 1,12 do
turtle.select(i)
end
end
end
end

function selectDirt()
turtle.select(14)
if turtle.getItemCount(14) == 1 then
turtle.select(1)
while not turtle.compareTo(14) do
for i = 1,12 do
turtle.select(i)
end
end
end
end

function placeLadder()
selectLadder()
if not turtle.place() then
selectDirt()
moveForward()
turtle.place()
turtle.back()
selectLadder()
if not turtle.place() then
return false
end
end
end

function torch()
if glow == 6 then
turtle.digDown()
turtle.select(13)
turtle.placeDown()
glow = glow-glow
end
end
print "Place ladder instance in slot 15, Fuel in 16, and dirt/stone in 14. Torches go in 13."
print "When only one ladder or filler is in their slots, program will look elsewhere."
print "How deep do you want to mine? (z)"
z = io.read()
z = tonumber(z)
print "Press any key to run."
os.pullEvent("char")

for i = 1,z do
turtle.digDown()
turtle.down()
refuel()
end
moveForward()
refuel()
turtle.turnLeft()
turtle.turnLeft()
for i = 1,z do
if not placeLadder() then
–break
end
torch()
selectDirt()
turtle.placeDown()
mineUp()
refuel()
glow = glow+1
end

"stairshaft"
Spoilerglow = 0
function moveForward()
repeat
turtle.dig()
until turtle.forward()
end

function refuel()
if turtle.getFuelLevel() <= 5 then
turtle.select(16)
turtle.refuel(1)
end
end

function seal()
if not turtle.detectDown() then
turtle.select(15)
turtle.placeDown()
end
end

function mine()
turtle.digDown()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.down()
turtle.down()
end

function torch()
if glow == 6 then
turtle.turnRight()
turtle.dig()
turtle.select(14)
turtle.place()
turtle.turnLeft()
glow = glow-glow
end
end

print "Fuel in slot 16, dirt/stone in 15, and torches in 14."
print "How deep do you want to go? This isn't about length, just depth."
z = io.read()
z = tonumber(z)
print "Press any key to run."
os.pullEvent("char")

for i = 1,z do
refuel()
mine()
seal()
moveForward()
glow = glow+1
torch()
end

"mine"
Spoiler–declaring variables
turtdir = 0
glowy = 0
glowx = 0
print "This will mine a predefined pattern of blocks. Use after vertshaft or stairshaft."
print "All values are positive integers."
print "Fuel in slot 16, torches in slot 15."
print "How wide is this mine?(x) !MUST BE AN ODD NUMBER!"
x = io.read()
x = tonumber(x)
print "How far forward? (y)"
y = io.read()
y = tonumber(y)
y = y-1
print "How high? (z)"
z = io.read()
z = tonumber(z)
z = z-1
print "Press any key to run."
os.pullEvent("char")
– origins
sxo = x/2
sxo = math.ceil(sxo)
syo = y/2
syo = math.ceil(syo)
– sxo and syo is used to find the turtle's start point. It is crucial that x be odd for this to work right.
xo = 0
yo = 0
zo = 0
setup = sxo/2
setup = math.ceil(setup)
– declaring functions
function checkFuel()
if turtle.getFuelLevel() <= z*2+2 then
turtle.select(16)
turtle.refuel(1)
end
end

function moveForward()
checkFuel()
repeat
turtle.dig()
until turtle.forward()
end

function mineUp()
if zo == 0 then
for i = 1,z do
checkFuel()
repeat
turtle.digUp()
until turtle.up()
zo = zo+1
end
end
end

function mineDown()
if zo ~= 0 then
repeat
checkFuel()
repeat
turtle.digDown()
until turtle.down()
zo = zo-1
until zo == 0
end
end

function checka()
if turtle.getItemCount(14) >= 1 then
turtle.turnLeft()
for i = 1,yo do
moveForward()
checkFuel()
end
for i = 1,14 do
turtle.select(i)
turtle.drop()
end
turtle.turnLeft()
turtle.turnLeft()
for i = 1,yo do
moveForward()
checkFuel()
end
turtle.turnLeft()
turtle.select(1)
end
end

function checkb()
if turtle.getItemCount(14) >= 1 then
turtle.turnRight()
for i = 1,yo do
moveForward()
checkFuel()
end
for i = 1,14 do
turtle.select(i)
turtle.drop()
end
turtle.turnLeft()
turtle.turnLeft()
for i = 1,yo do
moveForward()
checkFuel()
end
turtle.turnRight()
turtle.select(1)
end
end

function torch()
if glowx > 6 or glowx < 0 then
glowx = 0
end
if glowy == 5 and glowx == 6 then
turtle.turnLeft()
turtle.turnLeft()
turtle.select(15)
turtle.place()
turtle.turnRight()
turtle.turnRight()
glowx = glowx-glowx
end
end

– setting up to align
turtle.turnLeft()
for i = 1,setup do
mineUp()
moveForward()
mineDown()
moveForward()
end
turtle.turnLeft()
turtle.turnLeft()
mineUp()
mineDown()
for i = 1,setup do
moveForward()
end
for i = 1,setup do
moveForward()
mineUp()
moveForward()
mineDown()
end
– and finally, on line 147, we have reached the main loop.
for i = 1,syo do
turtdir = 1
checkb()
turtle.turnLeft()
moveForward()
yo = yo+1
if glowy == 6 then
glowy = glowy-glowy
end
glowy = glowy+1
turtle.turnLeft()
for a = 1,sxo-1 do
mineUp()
moveForward()
glowx = glowx+1
xo = xo+1
mineDown()
moveForward()
xo = xo+1
glowx = glowx+1
torch()
end
turtdir = 2
checka()
turtle.turnRight()
mineUp()
mineDown()
moveForward()
yo = yo+1
if glowy == 6 then
glowy = glowy-glowy
end
glowy = glowy+1
turtle.turnRight()
for b = 1,sxo-1 do
mineUp()
moveForward()
xo = xo-1
glowx = glowx-1
mineDown()
moveForward()
xo = xo-1
glowx = glowx-1
torch()
end
mineUp()
mineDown()
end
if turtdir == 1 then
turtle.turnLeft()
for i = 1,yo do
moveForward()
checkFuel()
end
for i = 1,16 do
turtle.select(i)
turtle.drop()
end
end
if turtdir == 2 then
turtle.turnRight()
for i = 1,yo do
moveForward()
checkFuel()
end
for i = 1,16 do
turtle.select(i)
turtle.drop()
end
end

Information that may or may not be important

SpoilerBugs: :P/>/>
"mine"
Do not enter an even number like "2" as "x," weird things happen.

Will probably mine a few too many blocks in the first row. Unsure how to fix. If it mines WAY too many, report it, in addition to the variables you told it.

No more known bugs, report any you find, and I will try to fix them, or at least warn other people.

Reccomended:
Don't enter ridiculous values in the turtle that I may not have prepared for. The turtle will still spill excess, and it only checks after every row, not during. Try to keep every row (x * z) under 100 blocks to minimize that risk. 25 x, 4 z, and 1,000,000 y works fine. (until it leaves the chunk limit) (!Statement Not Tested!)

I'm out of ideas, by the way. If someone thinks of a good turtle program that they want me to try making, tell me. Bugs? Tell me all about it. Happy? I'd love to hear it. I hope you enjoy this program as much as I enjoyed making it for you. :D/>/>
–Lettuce
Matrixmage #2
Posted 29 August 2012 - 06:52 AM
Seems like some pretty good programs, I'll check them out and report any unknowen bugs I find (if any of course :)/>/> )

P.s. with your "mine" program mining too few many blocks, I think I read somewhere that it had something to do with a bug, but you should double check about that
RoseRoar #3
Posted 30 August 2012 - 12:30 AM
First off, let's be clear here, I am brand new to ComputerCraft and Turtles. >.< Seems to be a bug in the mining program, and, honestly, I have no idea how to fix. Seems the program, or Turtle, fails at handling lava. I ran the Mining program and it zipped off, ran into the side of a lava pool, and vanished utterly. >.<

Which is Turtle #2 to vanish, the first I, thinking it wouldn't work so I typed it for fun, after giving it a stack of Coal Coke, Excavate 1024. Never saw that Turtle ever again, can't even figure out where it went. :)/>/>

But, back to the initial problem. Lava is an issue for any deep mine when one is hunting for diamonds, so if you can think of a way for it to handle lava, be really cool to see it coded to handle it.
Lettuce #4
Posted 30 August 2012 - 01:43 AM
Seems like some pretty good programs, I'll check them out and report any unknowen bugs I find (if any of course :)/>/> )

P.s. with your "mine" program mining too few many blocks, I think I read somewhere that it had something to do with a bug, but you should double check about that
First off, let's be clear here, I am brand new to ComputerCraft and Turtles. >.< Seems to be a bug in the mining program, and, honestly, I have no idea how to fix. Seems the program, or Turtle, fails at handling lava. I ran the Mining program and it zipped off, ran into the side of a lava pool, and vanished utterly. >.<

Which is Turtle #2 to vanish, the first I, thinking it wouldn't work so I typed it for fun, after giving it a stack of Coal Coke, Excavate 1024. Never saw that Turtle ever again, can't even figure out where it went. B)/>/>

But, back to the initial problem. Lava is an issue for any deep mine when one is hunting for diamonds, so if you can think of a way for it to handle lava, be really cool to see it coded to handle it.

Matrixmage, I hope you like it, it was a lot of work. Please report anything you find, in PMs or this topic. I would really appreciate it. If I don't know about it, I can't fix it. I assume that "mine" has a variable issue, which is why it mines the very first row weird. After that though, it's fine. I wouldn't have posted it with two errors if I thought they were major unavoidable issues.

RoseRoar, technically that's not a bug, it was never designed specifically for lava. I don't know how I might go about fixing that, but I'll look around.
I did try to make a safeguard for that. You see, when the program runs its course, and mines the last block, it's supposed to return. It also returns (and stores items) when it gets full. So I did prepare somewhat for the inevitable fire and brimstone, as your turtle should rarely lose itself or your items.

What were your variables, do you remember? If it was completely crazy, the turtle: a.) left the chunk boundary (probably what happened with excavate 1024) or b.) more likely, it ran out of fuel and is just sitting there somewhere. You should have seen the turtle eventually otherwise. It is a known issue that turtles disappear when they leave the chunk boundary. That's a ComputerCraft bug. Oh! c.) if you turned off the game, it stopped. Another Computercraft bug. In which case, it's still in the mine.
Cloudy #5
Posted 30 August 2012 - 11:18 AM
b.) It is a known issue that turtles disappear when they leave the chunk boundary. That's a ComputerCraft bug. Oh! c.) if you turned off the game, it stopped. Another Computercraft bug. In which case, it's still in the mine.

b.) That was fixed in ComputerCraft 1.4 as far as I know. I couldn't reproduce it after putting in a fix. By all means if you can reliably reproduce it with instructions let me know, and I'll fix it if it does exist.

c.) Not a bug - it's just how it works. We are looking to change that for future versions though.
Luanub #6
Posted 19 September 2012 - 11:35 AM
But, back to the initial problem. Lava is an issue for any deep mine when one is hunting for diamonds, so if you can think of a way for it to handle lava, be really cool to see it coded to handle it.

Lava should not be an issue. Turtles are not affected by liquids, they will move through lava like it isn't there.
slango20 #7
Posted 07 October 2012 - 01:41 PM
Put the code in code tags please, it is hard on my eyes to see code that's not color rendered
[code]
Lettuce #8
Posted 08 October 2012 - 12:35 AM
This post was made when I was relatively new to the forums. I forgot/didn't know about code tags at the time. I'll be re-posting this at a later point in time, for undisclosed reasons. I'm sorry it hurts your eyes. I've now posted 160 - some times at time of writing, so I know about them, I just never looked to see if I code tagged this.

I still stand by this program though, and I use it myself every time I play ComputerCraft.
peteleeb44 #9
Posted 13 October 2012 - 01:33 PM
Hi all. I read you were concerned with lava. If you keep your mine shaft a reasonable size, i don't go over a 9x9 block, then place a water block somewhere. No lava and chances are you will get some obsidian out of it too.

Just sayin.
ChunLing #10
Posted 13 October 2012 - 03:59 PM
It also obviates the need for autoplacement of ladders (and obviates the utility of placing torches). For best results, place the water block on the top of a flat layer that the turtle will mine away, and the water stream will fill pretty much the entire shaft.

I've found that the[Turtle] Replacement for `go` utility posted bytele_iz_dva4a can handle these kinds of basic tasks. My own version is better suited to my needs (and can be activated and run remotely).

A note on code, the function:
function moveForward()
repeat
turtle.dig()
until turtle.forward()
end
is serviceable in most cases, but it is a little inefficient and has a serious flaw when encountering bedrock or a mob. First, you should use a while turtle.forward() test so that it tries to move the turtle first, and only digs if it can't move. Second, you need some kind of check to prevent an infinite loop if it hits something that blocks movement but can't be mined. Third, you need a startup program that lets you control your turtle remotely, so you can recover it if it ends up stuck against bedrock at the bottom of a pool of lava :)/>/>
Lettuce #11
Posted 13 October 2012 - 04:58 PM
peteleeb44, ChunLing, thank you for trying to help everyone avoid lava. Your methods clearly work, and I don't know how to make the turtle avoid it, so some player intervention is needed, I suppose.

ChunLing, thanks for teaching me a new word. I had to look that up in a dictionary to know what you meant. Obviate essentially means "to circumvent danger" which is fitting in a discussion about lava, but why are ladders and torches dangerous? Both help. One prevents a nasty fall, the other prevents a nasty explosion.

While and repeat will do basically the same thing, in fact repeat is probably about a tenth of a second faster, because it breaks before checking if it can move, since there's probably stone in front of it. If a mob steps in front of it, it will probably move eventually, and the turtle would continue like nothing happened. I advise digging at least one block above the bedrock layer though. You still get your diamonds, and that deep down lava is surprisingly rare. I have a mine 11 blocks wide, 5 blocks high, and probably 200 blocks deep by now, and only encountered one lava flow, which was easily fixed.

It sounds like you have some good ideas for this program though, and I'd like to read them. Please PM me, if I use any of your ideas in the big bugfix I have planned, I'll credit you.

–Lettuce
ChunLing #12
Posted 13 October 2012 - 06:45 PM
The need for ladders is what is dangerous (i.e. a long fall). But the other usage, "obviating the utility/benefit/beauty/truth/etc." is slightly ironic, there merely to provide poetic balance. Though, in truth, water also obviates the need for torches, since hostile mobs can't spawn in water.

I also generally prefer repeat until to while do end, but in the case of a general movement function (as opposed to a specialized mining function), it makes sense to test for movement before attempting to break, particularly as when movement fails I detect to see if there is a block before breaking it, so that I can tell the difference between a gravel fall that can be overcome with a few more tries, bedrock which cannot be mined no matter what, and a mob that must be attacked (or out-waited) rather than mined.

I don't have a specialized mining function or program, I just use my general movement/building functions (particularly gox, based on tele_iz_dva4a's advanced go replacement program). You can find the full code of my basic turtle control program in the post User Friendly Program combining several useful functions.
Lettuce #13
Posted 13 October 2012 - 07:22 PM
I'd prefer to keep my programs working for "out of the box" ComputerCraft, and not to use API's that players may not have, just to spare myself some frustration.

I think moveForward() is efficient enough, and I'd rather the turtle be perfectly harmless even if the player or his/her precious, precious pig steps in front of it. I don't know how to detect bedrock, or lava. But thank you for trying to help.

–Lettuce
ChunLing #14
Posted 13 October 2012 - 07:54 PM
The programs I posted there have that exact goal in mind, being fully functional without needing any additional API's or knowledge of the system. Using GoX properly requires an understanding of loops (well, you can use it without the loops but the loops are the best part of the program), but only at the simplest level. I further designed them to run without command line parameters, but changed the turtle package to allow them so that you can have a turtle always enter remote control mode on startup.
Czarified #15
Posted 18 October 2012 - 03:08 AM
Hello. Your program's seem like exactly what I'm looking for. I installed them on my server, but when I try and run them (inputting the commands and values correctly and placing objects where they're supposed to go), my turtle doesn't do anything.

The command window says

mine:34: Attempt to compare number
with string expected. got number

However after that it just sits there. What am I doing wrong?

Could this have something to do with the check.fuel() function? I have fuel disabled on my server.
Lettuce #16
Posted 18 October 2012 - 03:24 AM
Czarified, I received a PM from someone else a while ago, who was having trouble with something completely different, as a result of copy/pasting wrong. He accidentally cut off a small portion of the code. Please copy the code again, and if you are still having trouble, PM me. I'll be glad to help.
ahbeef #17
Posted 19 October 2012 - 04:50 AM
getting an error on line 9 saying "stairshaft:9: attempt to call nil"
what do i do?
also: where are slots 14, 15, and 16? I am very confused about where to put the coal, torches, and cobble.
ahbeef #18
Posted 19 October 2012 - 04:54 AM
P.S. i did not copy and paste, because copying and pasting was giving me issues, so i copied it by hand and triple checked for spelling errors.
P.P.S. this is about the stairshaft one, sorry for not mentioning that earlier.
Also, is there a way to activate this using a wireless mining turtle and a computer with a wireless modem attached to it from the computer? if so, please tell me i would love to do that!
Thanks for the help!
ChunLing #19
Posted 19 October 2012 - 08:03 AM
Well, attempt to call nil errors are almost always from a typo, I bet the line is turtle.getFuelLevel(). You probably didn't capitalize the F or the L. Lua is case sensitive, but often our "proofing" eyes are not.

I'm thinking of adding a "shell.run("input line")" command to my remote turtle. Or rather someone else thought of it and I don't object. But I find it simpler to just use the gox command system for most things, and remote support for that is already built in. The gox system (totes props to tele_iz_dva4a, the original author) is very flexible even though the command strings are somewhat limited.
Lettuce #20
Posted 19 October 2012 - 07:12 PM
ahbeef, because you said you didn't know where slots 14, 15, or 16 were, I'm assuming you use CC 1.3, this code is meant for 1.4, where fuel came into play. Otherwise, the program simply doesn't work. If you are playing on tekkit, your version is 1.3. If you are not on 1.3, however, I welcome you to PM me.

No, I have not added wireless capabilities, but that is a very good idea, and I might add that at some point in time. Thank you for your inadvertent suggestion.