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

make mining turtle gravel resistant.

Started by bobthegear, 07 August 2013 - 09:57 AM
bobthegear #1
Posted 07 August 2013 - 11:57 AM
i have a problem with my program i want to use for mining. i tried to make my turtles resistant to graveldrops, because otherwise they wont move 50 blocks forward.
But if gravel falls in front of the turtle it will dig but not move forward anymore.
here is my code: http://pastebin.com/jWNnb1mv

or is there a better way than my if-condition?

i just realized: if i would remove the forward in line 13 and replace it between line 4 and 5 would that do the trick?

greets, Bob
Lyqyd #2
Posted 07 August 2013 - 07:48 PM
Split into new topic.
MR_nesquick #3
Posted 07 August 2013 - 08:14 PM
this is a endless loop, you need to add x = x + 1 to make it stop after 10 cycles

local x = 1
repeat turtle.dig(i) 
until x = 10

or you can do is remove 'function dete()' and add 'turtle.dig()' on line 12 and make the 'if turtle.detect() == true then' while turtle.detect() do'


function FW()
turtle.digUp()
turtle.digDown()
turtle.dig()

while turtle.detect() do --will loop until there is nothing in front of it
  turtle.dig()
  sleep(.5) --sleep for 0.5 sec so the gravel got time to fall 
end  

turtle.forward()
end
CCJJSax #4
Posted 07 August 2013 - 08:14 PM
Here is a small and simple program that should dig any amount of gravel in that column.


while turtle.detect() == true do	-- this will detect if there is a block in front.  
	  turtle.dig()				  -- this will dig
	  sleep(0.4)		   -- this will wait for .4 seconds as gravel/sand falls
end					-- closes the while

Gravel takes time to fall. That is why you need the sleep(0.4)
campicus #5
Posted 07 August 2013 - 08:23 PM
My programs only sleep 0.1 of a second and runs fine
bobthegear #6
Posted 08 August 2013 - 02:01 AM
Thank you very much guys, helped a lot. That i forgot to add the x = x + 1 was silly. :)/>

I will use your code because its better than mine (don't have to dig ten times if there are only two gravel blocks). One question:
function FW()
turtle.digUp()
turtle.digDown()
turtle.dig()

while turtle.detect() do will loop until there is nothing in front of it
turtle
.dig()
sleep(.5) sleep for 0.5 sec so the gravel got time to fall
end

turtle
.forward()
end


would it better to remove the turtle.dig() in line 4 and put the while loop there? because the gravel needs some time to fall, like you pointed out.

greets Bob
MR_nesquick #7
Posted 08 August 2013 - 08:40 AM
it's better if you're leaving 'turtle.dig()' on line 4 because you don't need wait on the turtle to sleep for 0.5 sec every time it break's a block.
Lord_Spelunky #8
Posted 08 August 2013 - 09:58 AM
You should be careful because if it is not gravel say a zombie, you should probably attack as well, so it doesn't get stuck :)/>
Dejected #9
Posted 08 August 2013 - 07:06 PM
I Know You Already Got Your Answers But You Can Also Use This Inside A while Loop Or Somewhere

if not turtle.forward() then
   turtle.dig()
end
Marteen21 #10
Posted 08 August 2013 - 07:27 PM
I just usually rewrite the moving functions for example

function try_forward()
   while not turtle.forward() then
      turtle.dig()
   end
end
CCJJSax #11
Posted 08 August 2013 - 08:11 PM
I use this function in one of my programs. it does the following.

1: mines a block
2: mines column of falling blocks
3: pulls one slot of items from an inventory (if present)
4: Sucks up entities on the ground.
5: attacks a player/mob that gets in it's way (if present)
6: it also counts it's actions, I have those set in certain variables, so there are things in here that call other functions that you don't need to worry about.

This might be a bad way to do this. I have fiddled with this code many times, I recently had to format my HDD and reinstall windows, so I don't know if this is my latest version. But as far as I know it's functional.
also I have been preoccupied so it's been awhile since I've seen this program.

local function checkdig()
[indent=1]x = 0[/indent]
[indent=1]attacknumber = 0 -- counts the amount of turtle.attack() actions[/indent]
[indent=1]if turtle.detect() == true then -- checks for block[/indent]
[indent=2]if turtle.suck() == true then -- checks for an inventory with something in it, if nothing is in the inventory, it will mine[/indent]
[indent=3]lastbutton = "got an item from inventory" -- think of last button as a print().  It's called elsewhere.  [/indent]
[indent=3]infoget = false -- think of this as another print()[/indent]
[indent=2]elseif turtle.detect() and turtle.dig() == true then [/indent]
[indent=3]x = x+1[/indent]
[indent=3]lastbutton = "mined "..x.." blocks"[/indent]
[indent=3]infoget = false[/indent]
[indent=3]movementdisplay() -- this is where my "lastbutton" and "infoget" are called[/indent]
[indent=3]sleep(.5)[/indent]
[indent=3]while turtle.dig() == true do -- this section mines the gravel[/indent]
[indent=4]x = x+1[/indent]
[indent=4]lastbutton = "mined "..x.." blocks"[/indent]
[indent=4]movementdisplay()[/indent]
[indent=4]sleep(.5)[/indent]
[indent=3]end[/indent]
[indent=2]elseif turtle.detect() and turtle.dig() == false then -- detects if it can break the block in front of it.[/indent]
[indent=3]lastbutton = "attempted to break block"[/indent]
[indent=3]infoget = "failed to break"[/indent]
[indent=3]movementdisplay()[/indent]
[indent=2]end[/indent]
[indent=1]elseif turtle.detect() == false then -- if there isn't a block[/indent]
[indent=2]while turtle.attack() == true do -- checks to attack[/indent]
[indent=3]turtle.attack()[/indent]
[indent=3]attacknumber = attacknumber+1[/indent]
[indent=3]lastbutton = "attacked: "..attacknumber[/indent]
[indent=3]infoget = false[/indent]
[indent=3]movementdisplay()[/indent]
[indent=2]end[/indent]
[indent=2]if turtle.suck() then  -- sucks from the ground[/indent]
[indent=3]lastbutton = "sucked up an entity"[/indent]
[indent=3]while turtle.suck() == true do[/indent]
[indent=3]turtle.suck()[/indent]
[indent=2]end[/indent]
[indent=1]elseif turtle.suck() == false and x == 0 and attacknumber == 0 then -- if nothing was there[/indent]
[indent=2]lastbutton = "had nothing to mine"[/indent]
[indent=2]infoget = false[/indent]
[indent=1]end[/indent]
end
colt_419 #12
Posted 09 August 2013 - 08:22 AM
I was running into the same problem with gravel so i fixed it like this, I know its kind of long but i think its simpler than all the variables and stuff.

function gravelCheck()
turtle.select(2) --Put gravel in slot 2, or put gravel in anyslot and change number.
if turtle.compare(2) == true then
  turtle.dig()
  gravelCheck2()
end
end
function gravelCheck2()
turtle.select(2)
if turtle.compare(2) == true then
  turtle.dig()
  gravelCheck()
end
end
Then just put:
gravelFix()
gravelFix2()
Main() - or whatever your main function is called.
Lyqyd #13
Posted 09 August 2013 - 10:27 AM
The above is an example of a bad (and probably ineffective) way of coding gravel and sand resistant movements. It really is a simple matter of looping an attempt to move forward until it succeeds and digging/attacking until it succeeds or you decide it's bedrock in front of the turtle.
DreadKyller #14
Posted 09 August 2013 - 02:03 PM
The simplest code I can think of is a code I used in a quarry system I setup. The turtle will not move forward if an entity is in the way, not just a block, so you don't need to use a sleep to delay the checking, the following code has never failed me and I've used it extensivly, it also prevents issues if a mob or player were to walk in front of the turtle.


local a = 0
while not turtle.forward() do
  turtle.dig()
  a = a + 1
  if a > 20 then
    -- Hit Bedrock
  end
end

If you know you're not going to hit bedrock, you can ommit the a variable and it's uses.