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

[Turtle] Help with my Mining code

Started by bioncleboy, 30 December 2012 - 01:56 PM
bioncleboy #1
Posted 30 December 2012 - 02:56 PM
This will be my first program with lua. I think that computercraft and turtles are a great way of learning a programming language.

It would be great if Pro could read over my code and give me some suggestions.

What i am trying to make is a turtle that digs a 2 by 1 shaft for the distance given and detects for gravel.

Code is in the spoiler:
Spoiler

local function refuel()

-- refuel: checks the fuel level, if it is not unlimited or if there is fuel/power left it does nothing, otherwise it selects slot one and refuels from it

local fuelLevel = turtle.getFuelLevel()
if fuelLevel ~= "unlimited" or fuelLevel > 0 then
turtle.select(1)
turtle.refuel(1)
end
end

local function twoByOne()
--the actually digging for the turtle
while turtle.detect()==true do
  turtle.dig()
  if turtle.detect()~=true then
        turtle.forward()
        sleep(0.5)
  end
while turtle.detectUp()==true do
  turtle.digUp()
  if turtle.detectUp()~=true then
  end
end
--##CREDIT TO mikey53591 ON THE CC FORUMS## This tells the turtle to keep digging until there are no blocks detected then ends the cycle--

-- asks the user for the distance to dig

print "How far forward? "
y = io.read()
y = tonumber(y)
i = 1

for i = 1, y, 1 do

-- torch placer

if i%7 == 0 then
turtle.select(2)
turtle.placeUp()
end

-- main loop that runs the functions until the distance required is meet

refuel()
sleep(0.25)

twoByOne()
sleep(0.25)

end



EDIT 1: Thanks mikey53591 for the gravel detect code.
EDIT 2: Water/lava detection doesn't work, water and lava count as blocks (face-palm)
EDIT 3: Added a torch placer, once every 7 blocks
mikey53591 #2
Posted 30 December 2012 - 05:12 PM
Ok, i'm not a pro, but i do have some suggestions that i've found to work pretty well. donno if it's more efficent, but hey. it's obvious and easy to read.

First, change this:
Spoiler

if not fuelLevel == "unlimited" or fuelLevel > 0 then

to

Spoiler

if fuelLevel ~= "unlimited" or fuelLevel > 0 then

Much easier to understand if-then-else statements.

To add in the gravel detection, change this:
Spoiler

local function twoByOne()
--the actually digging for the turtle
turtle.dig()
sleep(0.5)
forward()
turtle.digUp()
end

to

Spoiler

local function twoByOne()
--the actually digging for the turtle
while turtle.detect()==true do
  turtle.dig()
  if turtle.detect()~=true then
	turtle.forward()
	sleep(0.5)
  end
while turtle.detectUp()==true do
  turtle.digUp()
  if turtle.detectUp()~=true then
  end
end
--This tells the turtle to keep digging until there are no blocks detected then ends the cycle--

Those would be the things i would change, but i could be a bit off since i'm very new to Lua. By all means, have someone else check my changes :o/>
ChunLing #3
Posted 30 December 2012 - 07:49 PM
if fuelLevel ~= "unlimited" [i]and[/i] fuelLevel [i]> 1 [/i]then -- you changed the basic logic
local function twoByOne()-- hmmm, can't figure out what this is supposed to do that forward doesn't handle already
  turtle.digUp() --just that, far as I can tell
end
local function liquidDetect() --reacts to air too, that okay?
You can probably remove most of the sleeps in this without any problems.
bioncleboy #4
Posted 01 January 2013 - 06:51 AM
Thanks guys :)/> I have basic experience in a bunch of languages but i started writing this code with no knowledge of LUA. Both of you guys have great ideas, i will update my post and give credit.

thanks :)/>,

cam
bioncleboy #5
Posted 01 January 2013 - 06:56 AM
if fuelLevel ~= "unlimited" [i]and[/i] fuelLevel [i]> 1 [/i]then -- you changed the basic logic
local function twoByOne()-- hmmm, can't figure out what this is supposed to do that forward doesn't handle already
  turtle.digUp() --just that, far as I can tell
end
local function liquidDetect() --reacts to air too, that okay?
You can probably remove most of the sleeps in this without any problems.

I ran it ingame multiple times, the liquid detect doesnt work, water and lava count as blocks (facepalm)
ChunLing #6
Posted 01 January 2013 - 07:06 AM
Source blocks do, anyway. Flowing liquid shouldn't.
bioncleboy #7
Posted 18 January 2013 - 11:36 AM
anyone else have any ideas?