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

Foogle Proof Mining Program

Started by Foogles, 27 July 2015 - 09:42 PM
Foogles #1
Posted 27 July 2015 - 11:42 PM
Hello everyone. I am just getting started with computer craft and I love it! I have been refraining from using others shared programs with my turtles so that I can learn more about Lua and Computer Craft. I am also playing in survival, not testing anything in creative! :)/>

Since I started two days ago, I have written a shaft mining program that suits my purposes very well. It is capable of automatically grabbing lava from the environment while it mines, and using it to refuel itself. It also builds an enclosure filling in any gaps and liquids around the shaft, and replacing ores it mines with cobblestone, so that it can tunnel through water, lava, and caves in such a way that a player can go in behind it without risk of burning or getting blown up. It also has a return function. After it mines a specified shaft length, it will come back and re-orient itself exactly as it was left.

Foogle Proof Mining Program Version 1.3 (pastebin link)
  • mines 1x2 shafts of configurable distances (via command line argument)
  • places inventory in chest underneath starting position when inventory is full
  • places inventory in chest underneath starting position at conclusion of trip
  • keeps bucket and torches in its inventory
  • automatically returns to starting position
  • mines nearby ores within 1 block of the shaft
  • automatically obtains fuel (lava) from the environment
  • creates a safe shaft that a player can walk through
  • replaces adjacent water, lava, and air blocks with cobble to make shaft walls
  • gravel proof: detects whether it was able to move into a spot – if not, it mines there until it can move. Its count will not be negatively impacted by encountering gravel, and it will remain at the correct height.
  • places torches every 14 blocks. Due to its tunnel building skills, water will not destroy them to my knowledge.
Let me know what you would like to see in future updates, and let me know your thoughts and opinions on the code/performance! :)/>


Changelog:

Spoiler

Foogle Proof Mining Version 1.3: link
  • Turtle now empties out unneeded items at end of return trip
  • Turtle is now truly 100% gravel proof to my knowledge
  • Turtle now places torches every 14 blocks. Due to its tunnel building skills, water will not destroy them to my knowledge.
  • no longer places random blocks when it runs out of cobblestone
Let me know what you would like to see in future updates, and let me know your thoughts and opinions on the code/performance! :)/>

Foogle Proof Mining Version 1.2: link
  • Added CheckInv() and DumpInv()
  • Turtle now checks to see if it has an empty inventory slot after each search
  • Turtle now returns to the start and dumps the inventory (minus buckets and lava buckets)
  • A chest beneath the turtles starting location (where it was placed) will collect the dropped items
Version 1.1: link
  • Updated the item searching functions to grab the first (not last) stack located (special thanks to metron80)
  • Fixed an issue that crashed the program in the Continue() function (special thanks to Lion4ever)
  • Fixed an issue where the turtle would start mining at a higher y level if a block was placed beneath it during mining
Version 1.0: link


Planned Features:
  • automatically dispose of excess non-ore blocks
  • fill in empty wall spaces on return trip (this should only happen if it ran out of cobblestone)
  • automatically return if it is low on fuel (this should only happen if it is not mining at diamond level)
  • automatically place inventory (except buckets and torches) in designated chests
  • automatically place torches
  • custom shaft sizes (1x3, 2x2, 3x3)
  • use any stone/cobblestone type block for building, not just cobblestone
Known Bugs:
  • If it runs out of cobblestone, it may place a random item instead
  • May move down one block when in gravel (unknown cause)
The code is somewhat disorganized and I'm just a novice but I did heavily comment it so it should be clear how it works :)/>

Please take a look and/or try it in your turtle and share your thoughts. :)/> I'd love it if other people found this program useful.

If you fork it, please feel share your new code here.
metron80 #2
Posted 29 July 2015 - 04:44 PM
Amazing program! Works great. Might I suggest that instead of using this:

for i=1,16,1 do
    if (turtle.getItemDetail(i) ~= nil) then
	  if (turtle.getItemDetail(i).name == 'minecraft:lava_bucket') then
	    turtle.select(i)
	    i = 17
	  end
    end
end
You could use this:

for i=1,16 do
    if (turtle.getItemDetail(i) ~= nil) then
	  if (turtle.getItemDetail(i).name == 'minecraft:lava_bucket') then
	    turtle.select(i)
	    break
	  end
    end
end

Using "break" is better, because that just "breaks" the loop. Break also works in the "while" loop, so it is better practice. And you put i=1,16,1, which works fine, but if you leave out the last one, it defaults to 1. So that last part of i isn't really used very often.

But it works very well! Great job! :)/>
Foogles #3
Posted 29 July 2015 - 05:48 PM
Thank you for your interest and the advice. The entire program is original and I'm no pro in Lua or the turtle API, so your help is much appreciated.

I know there are a lot of mining programs available that are more robust than this, but I saw none that took advantage of lava as fuel. With all the lava pools near diamonds, I have only had to fuel my turtles running this program once. After a few mining trips, they've gone from 1k fuel to anywhere from 12k to 48k with no maintenance. My turtles also don't require designated slots for fuel, buckets, or other necessary items :)/>

I will implement your adjustments in version 1.1.
metron80 #4
Posted 29 July 2015 - 06:56 PM
That is a great idea, and I would have never thought of it. Lava is a great source of fuel for deep underground mining, especially with its abundance. Great thinking!
Lion4ever #5
Posted 29 July 2015 - 07:10 PM
This functions is a bit dangerous:

function Continue()
  turtle.dig()
  local x = turtle.forward()
  turtle.digUp()
  if (x == false) then
    Continue()
  end
end
This is a recusive call, but computercraft can only have a function depth of 255. So if you cant move for 0.4*3*255=1.2*255=306 seconds or 5 minutes and 6 seconds you program will have an error.
You could use a tail call like this:

function Continue()
  turtle.dig()
  local x = turtle.forward()
  turtle.digUp()
  if (x == false) then
    return Continue()
  end
end

or the best way to do it:

function Continue()
  local x
  repeat
  turtle.dig()
  x = turtle.forward()
  turtle.digUp()
  until x
end
Foogles #6
Posted 29 July 2015 - 10:21 PM
This is a recusive call, but computercraft can only have a function depth of 255. So if you cant move for 0.4*3*255=1.2*255=306 seconds or 5 minutes and 6 seconds you program will have an error.
You could use a tail

This is something I was not aware of. Using a tail call is good advice, considering that I have written a "recursive" function that doesn't actually require recursion, as each iteration can resolve itself before calling itself again without changing how it works.

Thank you very much for your concern and your well explained alternative blocks. I will make modifications to the Continue() function to resolve the issue you mentioned in version 1.1.

I will also be adding a pathfinding function for the turtle so that it will move around entities if (turtle.dig() == false and turtle.forward() == false).
Foogles #7
Posted 30 July 2015 - 11:01 PM
I am pleased to now share with you…

Foogle Proof Mining Version 1.1: link
  • Updated the item searching functions to grab the first (not last) stack located (special thanks to metron80)
  • Fixed an issue that crashed the program in the Continue() function (special thanks to Lion4ever)
  • Fixed an issue where the turtle would start mining at a higher y level if a block was placed beneath it during mining
Thank you to those of you who have tried it and shared your interest and advice :)/>
Foogles #8
Posted 02 August 2015 - 05:59 AM
Version 1.2 of Foogle Proof Mining is in! The program now features inventory management… the turtle will empty its inventory into a chest beneath the starting location when it is full!

Foogle Proof Mining Version 1.2: link
  • Added CheckInv() and DumpInv()
  • Turtle now checks to see if it has an empty inventory slot after each search
  • Turtle now returns to the start and dumps the inventory (minus buckets and lava buckets)
  • A chest beneath the turtles starting location (where it was placed) will collect the dropped items
The next version should contain support for arguments that will enable better customization of the shaft size/patterns.
Curlip #9
Posted 09 August 2015 - 08:06 AM
  • May move down one block when in gravel (unknown cause)

As a fix perhaps you could use to GPS (API) to test if the Y changes, you could then move up/down and replace the blocks underneath/above.

Secondly I made an edit that lets my turtle now place torches, in no way is it fuel efficient, however it works for me. it can be found on PasteBin

I am no Pro-Programmer, however I am happy to put forward my ideas.

Curlip
Edited on 09 August 2015 - 06:56 AM
Foogles #10
Posted 09 August 2015 - 07:55 PM
Thank you for your input and interest, Curlip.

I'm hesitant to enable the gps api because my primary focus for this project is to make an easy to use mining software that you can upload on turtles early in the game, before you have a network set up. This also makes it easier for newbies to ComputerCraft simply pastebin it in and go, because all it requires is one turtle.

As for the torch edit, I like what you did. I'll upload a patch to your version soon which enables the turtle to craft torches and to find them in any slot using a GetTorch() function. :)/>
Creator #11
Posted 09 August 2015 - 08:35 PM
Nice. Very useful program. Keep up the excellent work. ;)/>
Hawkertech #12
Posted 15 September 2015 - 05:56 AM
As per your post it does move down when it encounters gravel. but not just one block, it will move down 1 block each time it moves forward when it encounters gravel Mine came back about 6 blocks below the chest.
Foogles #13
Posted 19 January 2016 - 04:01 PM
Hey everyone! 3500 views, 1300 pastebin downloads on the latest version, and until now I had yet to fix the gravel issue, and yet to implement torch placing.

I stayed up late last night and pumped out Foogle Proof Mining Version 1.3.

In the latest version, the turtle now handles gravel flawlessly. I had already implemented a solution for gravel in front of the turtle.

However, the turtle failed to handle gravel properly if the gravel fell on it's head before it attempted to move up. Once I realized this is where the error was occurring, it was a simple matter of replacing turtle.up() with a recursive TurtleUp() function that utilizes the boolean returned by turtle.up() to determine if the turtle needs to continue mining blocks above it before it can move.

I also made it so that the turtle will not only dump excess items in a chest when its inventory overflows, but also when it finishes the trip.

In addition, as promised months ago, I implemented torch placing. If the turtle has a torch, it will locate it, and place it every 16 blocks along the mineshaft. This part was trickier than expected because it seems to always place the torch in a specific orientation, meaning that when the turtle is going one direction versus another, it would sometimes put the torch on the wrong wall and then mine that block, destroying the torch.

Also, as an experiment, I sent 16 mining turtles on 100 block long missions (Using the command: Mine 100) giving them only 1 bucket of lava. All turtles returned at the correct height level and did everything as expected, without running out of fuel.

Tonight I will implement a version of Lion4ever's compass program in order to place the torch securely without wasting any fuel by moving back to place the torch.

Be on the lookout for the new pastebin link for Foogle Proof Mining 1.3 within the next 12 hours!
Edited on 19 January 2016 - 03:02 PM
Foogles #14
Posted 20 January 2016 - 12:03 AM
As promised, here is Foogle Proof Mining version 1.3!

Foogle Proof Mining Version 1.3: link
  • Turtle now empties out unneeded items at end of return trip
  • Turtle is now truly 100% gravel proof to my knowledge
  • Turtle now places torches every 14 blocks. Due to its tunnel building skills, water will not destroy them to my knowledge.
  • Fixed mistake in which the turtle placed random blocks if it ran out of cobblestone

At this point, all known bugs have been fixed!

Let me know what you would like to see in future updates, and let me know your thoughts and opinions on the code/performance! :)/>
Edited on 19 January 2016 - 11:11 PM
BeanD #15
Posted 23 January 2016 - 09:27 PM
Getting Mine:63: Attempt to call nil - No idea what it means as I have no programming experience and I am just getting into this. any help would be awesome.
Foogles #16
Posted 24 January 2016 - 02:23 AM
BeanD, this program does not work for certain versions of ComputerCraft.

"Attempt to call nil" indicates that the program attempted to execute a function that does not exist.

Here is line 63:

if (turtle.getItemDetail(i) ~= nil) then

The only function that is called here is turtle.getItemDetail(). That function was added in ComputerCraft 1.6.4.

If you are using the Tekkit modpack, it is important to note that this program will not work in your turtle because that modpack uses an older version of ComputerCraft.

If you are using Tekkit, I'd recommend upgrading to Tekkit Legends, which is using ComputerCraftEdu 1.7.4, a more current and stable version that will work with my program.
Edited on 24 January 2016 - 01:25 AM
BeanD #17
Posted 24 January 2016 - 04:42 PM
Ahh that makes a lot of sense, thank you for this information.
Fridolin #18
Posted 29 February 2016 - 05:15 PM
I still have a problem with it:
If I, for example type in "Mine 10",
it works for a few blocks, then suddenly stops while looking to the right on the bottom block after coming down from an upper one and the turtle echos this error:
Mine:287: vm_error: java.lang.ArrayIndexOutOfBoundsException
sometimes, it only returns
java.lang.ArrayIndexOutOfBoundsException

Picture in Spoiler:
Spoiler

/edit: Okay…found out, i happens mostly if there is bedrock at any side the turtle is checking…
Edited on 29 February 2016 - 09:30 PM