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

Very simple mining program - Newbies will love this

Started by mymusicmanager, 21 May 2013 - 08:24 AM
mymusicmanager #1
Posted 21 May 2013 - 10:24 AM
Hello everyone!

First post here, so I'm a little excited..


Okay, this is a VERY simple and basic mining program. It can't go wrong :P/>

http://pastebin.com/9WuGM99n


Code:
Spoilerlocal times = ""
local min = 500
local Level = turtle.getFuelLevel()
local FuelNeeded






sleep (1)
print ("Turtle will mine 1 block in front, and one to its left. Please use Ctrl+t to terminate and move the turtle")
sleep (2)
print ("Mining turtle wants to dig a 2x2 tunnel. Agree? 1 = Ok, 2 = No")
local input = read()
if input == "1" then
print ("Starting the program…")
sleep (1)
print ("How long do you wish to tunnel?")
times = read()

FuelNeeded = times*4+1
print ("Fuel needed to dig is "..FuelNeeded)

if turtle.getFuelLevel() < FuelNeeded then
print ("Fuel level is: "..turtle.getFuelLevel())
print ("Turtle is low on fuel. Do you wish to refuel? y/n")
local event, param1 = os.pullEvent("char")
if param1 == "y" then
turtle.select(1)
turtle.refuel()

else
print ("!Too low fuel for program to initiate!")
print ("Turtle rebooting!")
sleep (2)
os.reboot()

end
end

for i = 1, times do
turtle.dig()
while turtle.detect() do
turtle.dig()
end
while not turtle.forward() do
turtle.dig()
end
turtle.digUp()
while turtle.detectUp() do
turtle.digUp()
end
turtle.turnLeft()
turtle.dig()
while turtle.detect() do
turtle.dig()
end
turtle.turnRight()
while not turtle.up() do
turtle.digUp()
end
turtle.turnLeft()
turtle.dig()
while turtle.detect() do
turtle.dig()
end
turtle.turnRight()
turtle.down()

end
times = times+1
turtle.turnLeft()
turtle.turnLeft()

for i = 1, times do
while not turtle.forward() do
sleep (1)
end
end
end
turtle.turnLeft()
turtle.turnLeft()



Basic idea is: You start the program :P/>

Next you agree to start
Then you enter how long it will tunnel
After that, you put fuel in if required

Best part is that it will return to its starting position once it's done mining.
Will also handle sand and gravel very nicely ;)/>

Again, very simple program.


How to get it:

* Make your mining turtle, and place it wherever you want to mine
* Open up the interface
* Type "pastebin get 9WuGM99n Dig" <—- this can be any name you want
* Press Enter
* Run the program, and enjoy the simple mining



I highly reccomend for everyone to install a chunk loader module if possible, as it's easy for the turtle to wonder off from the loaded chunks. It WILL stop, so be sure to either follow the turtle or to get a chunk loader on it ;)/>


Anyway, have fun with this thing

-MyMusicManager



Changelog
SpoilerV.0.2.1 - Shortened code by about 60 lines(!)
——————————————
V.0.2.0 - Major update! Will now have the option to drop all items into either pre-placed chest, or place one down when done mining. Thanks A LOT to UNOBTANIUM for helping me with my code. You're awsome, dude! :)/>
——————————————
v.0.1.6 - Will now build a bridge if no blocks below it. Will also always display current fuel
——————————————
v.0.1.5.1 - I forgot like 1 line of code :P/> Derp
——————————————-
v.0.1.5 - Tiny update with small improvements. Still a lot to do
——————————————-
v.0.1.4 - Woah, this is moving fast. Will now show how much fuel is needed based on the length you wish to tunnel
——————————————-
v.0.1.3 - More tweaks towards refueling. Bugs have been killed
——————————————-
v.0.1.2 - Minor tweaks, added handling for gravel and sand
——————————————-
v.0.1 - First release


Thanks A LOT to UNOBTANIUM for helping me so much with this code :)/>


WOA! 500 views in under a week? You guyes are awsome!
unobtanium #2
Posted 21 May 2013 - 11:10 AM
Hello there,

sweet little program you created there. I went over the code quickly and found some parts you might want to improve ;D

SpoilerIf you let the user chose from given options, it might be allways better to pull an event.

local id, key = os.pullEvent("key")
This gives back the key number of the pressed key. E.g. the number 1 on the keyboard is 2 for the pc. 2 is 3 and e.g. enter is 28.
This lets the player just use from the given options. You can detect a keypress with the following command:

if type(key) == "number" and key == 2 then -- if the number 1 key got pressed
-- do this
end


Refuel part: If the user writes "yes" it still does not refuel. Use the pull-event i showed you above ;D
Agree to start: This accually should work :s read() allways gives you a string but you are asking for a number. Change this to a pull-event as well.
Variables Level: This variables doesnt get changed if the user refuels the turtle. Even if the turtle has over 500 fuel in it, it shows less if it hasnt before.
This program doesnt handle falling sand and gravel. It has to detect if something is in front of it with a while loop and then digs it until it is gone.
mymusicmanager #3
Posted 21 May 2013 - 11:41 AM
Hello there,

sweet little program you created there. I went over the code quickly and found some parts you might want to improve ;D

SpoilerIf you let the user chose from given options, it might be allways better to pull an event.

local id, key = os.pullEvent("key")
This gives back the key number of the pressed key. E.g. the number 1 on the keyboard is 2 for the pc. 2 is 3 and e.g. enter is 28.
This lets the player just use from the given options. You can detect a keypress with the following command:

if type(key) == "number" and key == 2 then -- if the number 1 key got pressed
-- do this
end


Refuel part: If the user writes "yes" it still does not refuel. Use the pull-event i showed you above ;D
Agree to start: This accually should work :s read() allways gives you a string but you are asking for a number. Change this to a pull-event as well.
Variables Level: This variables doesnt get changed if the user refuels the turtle. Even if the turtle has over 500 fuel in it, it shows less if it hasnt before.
This program doesnt handle falling sand and gravel. It has to detect if something is in front of it with a while loop and then digs it until it is gone.


LIike I said, I'm quite the newbie on this kind of programming, but I'll look into this and try to improve the code. I know Direwolf20 has made some cool CC tutorials, I'll have a loot at those as well

Thanks for the feedback :)/>
mymusicmanager #4
Posted 21 May 2013 - 12:20 PM
Okay, Minor tweaks and small changes. Will now handle sand and gravel, even if it falls on top of the turtle
mymusicmanager #5
Posted 21 May 2013 - 01:41 PM
New update :P/>

Hopefully fixed the refueling issue
unobtanium #6
Posted 21 May 2013 - 02:04 PM
Wouldnt it better if they turtle asks for the length of the tunnel first and then would calculate the fuel needed? Would be length*4=neededFuelForExcavation
mymusicmanager #7
Posted 21 May 2013 - 02:24 PM
Let me experiment a bit with that, and see what I come up with
mymusicmanager #8
Posted 21 May 2013 - 03:17 PM
New update again. Still a lot to do, like forcing program to hold and wait for fuel if below required. However, that's something I have to do another day, another time.
unobtanium #9
Posted 21 May 2013 - 03:35 PM
I would add a while loop on line 29 to solve your refuel problem


if param1 == "y" then
turtle.select(1)
while turtle.getFuelLevel() < FuelNeeded do
turtle.refuel(1)
end
else
mymusicmanager #10
Posted 22 May 2013 - 08:52 AM
I was wondering, If I did something like this during the mining program:

local CheckDown = turtle.detectDown()


while CheckDown = false do
turtle.select(2)
turtle.placeDown()
end


or should I do

local CheckDown = turtle.checkDown()
while not CheckDown do
turtle.select(2)
turtle.placeDown()
end

This is to make a bridge if required. Slot 2 should be filled with cobblestone, of course

Which of thse would work for the best?
unobtanium #11
Posted 22 May 2013 - 11:28 AM
It would be like this:

turtle.select(2)
while not turtle.detectDown() do
turtle.placeDown()
end
Does the exact same thing yours did, but is it performance technical better (which accually doesnt matter because the turtle has allready build-in slowing down commands and animations).
A problem which occurs now is, that if the turtle has no blocks in slot 2 left, it tries to place down a block every tick until it gives a error code at some point ("without yielding" or something crazy). So you can also add a "sleep(0.5)" after the "turtle.placeDown()" which let the turtle wait have a second before checking again.
mymusicmanager #12
Posted 22 May 2013 - 01:45 PM
Howerer, is there not a way for me to let the turtle check if there is any items in slot 2? So that if there are no blocks in slot 2, it goes to slot 3?

However, this should really not be a problem because we are only going to place a one wide bridge to that we can get the turtle if anything happens. And how many 64 block long caves can i find? :P/>

Thanks for all your advice on code, you're a really cool coder :)/> Love your programs btw
unobtanium #13
Posted 22 May 2013 - 02:06 PM
Here is the turtle api, where everything the turtle can do as a turtle.<command> command is listed.

turtle.getItemCount(2) -- gives the amount of items in the slot back

Thank you :D/> I started making little programs like you are doing them as well. Some "old" members here in the forums gave me advices and helped me as well, so i try to give everybody a quick start and overview about turtle mechanics and tweaks.
mymusicmanager #14
Posted 23 May 2013 - 03:46 PM
I tried to make the turtle drop off items when done, but it did not work out that great… Maybe you could have a look through it, UNO, and maybe make it work? O.o

To auto-dropoff items, you can place a hopper under the turtle. That hopper can lead down to, say, an ender chest to a sorting system? :P/>
unobtanium #15
Posted 23 May 2013 - 04:12 PM
I tried to make the turtle drop off items when done, but it did not work out that great… Maybe you could have a look through it, UNO, and maybe make it work? O.o

What ya wanna do? Drop every items in the inventory?
[size=4]for i=1, 16 do[/size]
  if turtle.getItemCount(i) > 0 then
   turtle.select(i)
   turtle.drop()
  end
end
Change the 1 for the first slot and the 16 for the last slot to drop.
To auto-dropoff items, you can place a hopper under the turtle. That hopper can lead down to, say, an ender chest to a sorting system? :P/>
Hoppers arnt part of most fbt versions because they run with MC version < 1.5
mymusicmanager #16
Posted 24 May 2013 - 04:57 AM
I want people to have the option to either make the turtle auto-drop a chest and drop items into that
or to drop items into a pre-placed chest
or just ignore dropping any items

I have tried to declare a local value and reffering to that later on once done mining and moving, but the program did not want to pull the local value, and just ignored all of it.
unobtanium #17
Posted 24 May 2013 - 06:13 AM
You created a local chest within multiple if-statements. A local variables just "lives" or "exists" until the next end occurs (simple explanation).
Create the variable at the start of the program and just load it without the "local" if you fill it with information and it should work.

Try to minimize your code a little bit :s
Allways use th for-loop if you allready know what is going to happen. You can put the 32 lines of code in which you drop the whole inventory in the chest into simple 4 lines:

for i=1,16 do
turtle.select(1)
turtle.drop()
end
-- OR a quicker solution while it is running:
for i=1,16 do
if turtle.getItemCount(i) > 0 then
  turtle.select(1)
  turtle.drop()
end
end

You also should realize that if you are placeing a chest, that you still drop the whole inventory afterwards. You also dont need the Chest variable anymore, so you could re-use this one to shorten your program even more.


if Chest == 1 then -- place the chest
turtle.select(16)
turtle.place()
Chest = 2
end
if Chest == 2 then -- drop the inventory
for i=1,16 do
  if turtle.getItemCount(i) then
   turtle.select(1)
   turtle.drop()
  end
end
end
print("Done!")

It is accually bad, that i am showing you the whole code :s
Try to read up about the following commands, because learning it by yourself is much more fun and better.
- for loop
- while loop
- repeat
- functions
- break
- return
mymusicmanager #18
Posted 24 May 2013 - 09:57 AM
Thanks, will do ;)/>
mymusicmanager #19
Posted 24 May 2013 - 01:13 PM
Okay, cleared up a lot of code, and everything works perfectly. Still doing research on CC commands, and might include more features in the future
mymusicmanager #20
Posted 26 May 2013 - 08:04 PM
Turtle should not need any changes in the code for the 1.5.2 update. I hope 1.6 don't change too much stuff in code…

Anyway, few more imporvements. Also, thanks for over 1000 views :)/>
AlucaBR #21
Posted 29 January 2014 - 12:44 AM
have chunk loader module for minecraft 1.6.4? i play modpack FTB monster minecraft 1.6.4, but no have this module :(/> I need to load the chunks so that the turtle does not stop mining
HeffeD #22
Posted 29 January 2014 - 12:28 PM
have chunk loader module for minecraft 1.6.4? i play modpack FTB monster minecraft 1.6.4, but no have this module :(/> I need to load the chunks so that the turtle does not stop mining

You'll need MiscPeripherals for a chunk loading module.
http://www.computercraft.info/forums2/index.php?/topic/4587-cc153mc152-miscperipherals-33/

I haven't tried MiscPeripherals version for 1.6, but the chunk loader was completely broken in the previous version. If they're still broken in the 1.6 version, until RichardG starts working on MiscPeripherals again, I'd just use standalone chunk loaders from mods like ChickenChunks or Railcraft.