Jump to content




Help with gravel in branchmining program

turtle computer api

8 replies to this topic

#1 Typez00

  • New Members
  • 2 posts

Posted 11 July 2014 - 06:16 PM

I have been trying to make this branchmining program to work when hitting gravel. But when the turtle hits gravel the length of the shaft is decreased. So i need help with keeping the length of the shaft even when hitting gravel.

Here is the code:
local tArgs= {...}
if #tArgs ~= 1 then
print("Enter number of chests in slot one and put fuel in slot 2!")
return
end
local length = tonumber( tArgs[1] )
n= 20
function forward()
turtle.forward()
end

function left()
turtle.turnLeft()
end

function right()
turtle.turnRight()
end

function up()
turtle.up()
end

function down()
turtle.down()
end

function dig()
turtle.dig()
end

function digUp()
turtle.digUp()
end

local function fuel()
if turtle.getFuelLevel() < 100 then
turtle.select(2)
turtle.refuel()
turtle.select(1)
end
end

local function gravel()
dig()
sleep(0.25)
if forward() == false then
repeat
dig()
sleep(0.25)
until forward() == true
end
end

local function path()
for i= 1,n do
dig()
digUp()
n=n-1
if forward() == false then
dig()
n=n+1
end
end
end

local function back()
left()
left()
for i= 1,20 do
forward()
end
end

local function side()
dig()
turtle.select(1)
turtle.place()
turtle.select(3)
turtle.drop()
turtle.select(4)
turtle.drop()
turtle.select(5)
turtle.drop()
turtle.select(6)
turtle.drop()
turtle.select(7)
turtle.drop()
turtle.select(8)
turtle.drop()
turtle.select(9)
turtle.drop()
turtle.select(10)
turtle.drop()
turtle.select(11)
turtle.drop()
turtle.select(12)
turtle.drop()
turtle.select(13)
turtle.drop()
turtle.select(14)
turtle.drop()
turtle.select(15)
turtle.drop()
turtle.select(16)
turtle.drop()
turtle.select(1)
left()
gravel()
gravel()
gravel()
left()
end


for o= 1,length do
fuel()
path()
back()
side()
end

Thanks for the help! :)

#2 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 11 July 2014 - 06:35 PM

Moved to Ask A Pro.

#3 Kizz

  • Members
  • 99 posts
  • LocationLouisville, Kentucky

Posted 11 July 2014 - 06:41 PM

Please try and use the "["code"][/"code"]" tags. Makes it much easier.

local tArgs= {...}

if #tArgs ~= 1 then
print("Enter number of chests in slot one and put fuel in slot 2!")
return
end

local length = tonumber( tArgs[1] )
n= 20
function forward()
turtle.forward()
end

function left()
turtle.turnLeft()
end

function right()
turtle.turnRight()
end

function up()
turtle.up()
end

function down()
turtle.down()
end

function dig()
turtle.dig()
end

function digUp()
turtle.digUp()
end

local function fuel()
if turtle.getFuelLevel() < 100 then
turtle.select(2)
turtle.refuel()
turtle.select(1)
end
end

local function gravel()
dig()
sleep(0.25)
if forward() == false then
repeat
dig()
sleep(0.25)
until forward() == true
end
end

local function path()
for i= 1,n do
dig()
digUp()
n=n-1
if forward() == false then
dig()
n=n+1
end
end
end

local function back()
left()
left()
for i= 1,20 do
forward()
end
end

local function side()
dig()
turtle.select(1)
turtle.place()
turtle.select(3)
turtle.drop()
turtle.select(4)
turtle.drop()
turtle.select(5)
turtle.drop()
turtle.select(6)
turtle.drop()
turtle.select(7)
turtle.drop()
turtle.select(8)
turtle.drop()
turtle.select(9)
turtle.drop()
turtle.select(10)
turtle.drop()
turtle.select(11)
turtle.drop()
turtle.select(12)
turtle.drop()
turtle.select(13)
turtle.drop()
turtle.select(14)
turtle.drop()
turtle.select(15)
turtle.drop()
turtle.select(16)
turtle.drop()
turtle.select(1)
left()
gravel()
gravel()
gravel()
left()
end

for o= 1,length do
fuel()
path()
back()
side()
end

What specifically do you mean by "length of the shaft."

I can't tell what you're trying to do with this program.

Edited by Kizz, 11 July 2014 - 06:49 PM.


#4 ReconTurtle

  • Members
  • 26 posts

Posted 11 July 2014 - 09:39 PM

The easiest and best thing you can do to allow your turtle to take on all that pesky gravel is to change the function dig() to the following code:
function dig()
  while turtle.detect() do		  --This will continue to do the following action while there is a block in front of the Turtle
	turtle.dig()
  end
end

While you're doing that, you may want to change digUp() to do the same thing:
function digUp()
  while turtle.detectUp() do
	turtle.digUp()
  end
end

The combination of these two changes should take care of it all for you.
While I'm typing this out, I want to mention the power of the for loop. You have a messy bit of code at the end that deposits all the dug-up items in a chest, and instead of going through each slot like that, you can just use the following bit of code that will do the exact same thing, but it will be a lot easier to read:
for slot=3,16,1 do		  --This will start on slot 3, and go to slot 16, with increments of 1 between.
  turtle.select(slot)
  turtle.drop()
end


Let me know if any of this doesn't work for you, or if you need further clarification!
-ReconTurtle

Edited by ReconTurtle, 11 July 2014 - 09:43 PM.


#5 Typez00

  • New Members
  • 2 posts

Posted 12 July 2014 - 08:32 AM

Thanks ReconTurtle for the help! The program is now working the way a want it to do! The dig function works very good and the code that puts items in the chest also works good :) But the problem is the digUp function, if gravel is above the block the turtle is digging it just leaves the gravel and keeps going. I compensated for that by running the dig command on the way back.

Here is the updated code:

local tArgs= {...}
if #tArgs ~= 1 then
print("Enter number of chests in slot one and put fuel in slot 2!")
return
end
local length = tonumber( tArgs[1] )
n= 20
function forward()
turtle.forward()
end

function left()
turtle.turnLeft()
end

function right()
turtle.turnRight()
end

function up()
turtle.up()
end

function down()
turtle.down()
end

function dig()
while turtle.detect() do
turtle.dig()
end
end

function digUp()
while turtle.detectUp() do
turtle.digUp()
end
end

local function fuel()
if turtle.getFuelLevel() < 100 then
turtle.select(2)
turtle.refuel()
turtle.select(1)
end
end

local function path()
for i= 1,20 do
dig()
forward()
digUp()
end
end

local function back()
left()
left()
for i= 1,20 do
dig()
forward()
digUp()
end
end

local function side()
dig()
turtle.select(1)
turtle.place()
for slot=3,16,1 do
turtle.select(slot)
turtle.drop()
end
turtle.select(1)
left()
dig()
forward()
digUp()
dig()
forward()
digUp()
dig()
forward()
digUp()
left()
end


for o= 1,length do
fuel()
path()
back()
side()
end

Thanks again ReconTurtle for helping me!

#6 Inksaver

  • Members
  • 61 posts

Posted 13 July 2014 - 08:34 AM

The reason it does not quite work as expected is you have not allowed time for the gravel to fall
function digUp()
  while turtle.detectUp() do
	turtle.digUp()
	sleep(0.5)
  end
end

function dig()
  while turtle.detect() do
	turtle.dig()
	sleep(0.5)
  end
end
The use of sleep(interval) should do it

If mobs are a problem:

function attack()
  while turtle.attack() do --in case mob in front
    sleep(1.5)
  end
  while turtle.attackUp() do --in case mob above
    sleep(1.5)
  end
  while turtle.attackDown() do --in case mob below
    sleep(1.5)
  end
end
Then modify the forward function to cope with mobs AND gravel
function forward()
  while turtle.detect() do
	turtle.dig()
	sleep(0.5)
  end
  attack()
  turtle.forward()
end

Edited by Inksaver, 13 July 2014 - 12:01 PM.


#7 Danny12328

  • Members
  • 7 posts

Posted 22 July 2014 - 06:02 AM

You should try before the turtle moves forward, make it detect for blocks, above and infront of it and if it returns true have it dig where a block was detected.

#8 RabidxOtaku

  • Members
  • 3 posts

Posted 28 July 2014 - 11:43 PM

What I did was create functions to call open when I moved.

so instead of
turtle.dig()
turtle.forward()

I did
function goForward()
  while turtle.detect() do
    turtle.dig()
  end
  turtle.forward()
end

now replace detect with detectUp, detectDown, and I created a turnAround function. then you not only do less typing to move forward, but in situations such as gravel, it will grind through it. This is also easier to remember and useful if you do a system wide function :P

I hope this somewhat answers your question, while I'm good at coding, and I can really wip up concepts, I don't really have the mass knowledge for lua itself and copmutercraft functions, otherwise I'd give a better answer.

#9 RabidxOtaku

  • Members
  • 3 posts

Posted 28 July 2014 - 11:46 PM

View PostInksaver, on 13 July 2014 - 08:34 AM, said:

The reason it does not quite work as expected is you have not allowed time for the gravel to fall
function digUp()
  while turtle.detectUp() do
	turtle.digUp()
	sleep(0.5)
  end
end

function dig()
  while turtle.detect() do
	turtle.dig()
	sleep(0.5)
  end
end
The use of sleep(interval) should do it

If mobs are a problem:

function attack()
  while turtle.attack() do --in case mob in front
	sleep(1.5)
  end
  while turtle.attackUp() do --in case mob above
	sleep(1.5)
  end
  while turtle.attackDown() do --in case mob below
	sleep(1.5)
  end
end
Then modify the forward function to cope with mobs AND gravel
function forward()
  while turtle.detect() do
	turtle.dig()
	sleep(0.5)
  end
  attack()
  turtle.forward()
end

This works good, but I try to be conserative and original if you will, no disrespect to thread OP or quoted post OP. So what I would do is make a larger file size with clean and effecient programming, then only have it attack and or detect open a certain function so that you can conserver that 0.0001 GB of ram it uses to run a 10KB file. Plus its about person preference :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users