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

Auto miner help

Started by Football52, 14 May 2015 - 12:42 AM
Football52 #1
Posted 14 May 2015 - 02:42 AM
Basically I'm making a program that tells a turtle to dig out a specified area and height. The problem I am having is figuring out how to implement so the turtle returns to the starting point after finishing a layer? The way I want my program to work is so after the turtle finishes a layer, it returns to the spot it started and goes up 1 level and reruns.

The code I have so far is as followed

Auto slim farm


function forward()
   turtle.detect()
   turtle.dig()
   turtle.attack()
   turtle.forward()
end

function turnRight()
   turtle.turnRight()
   turtle.detect()
   turtle.dig()
   turtle.attack()
   turtle.forward()
   turtle.turnRight()
end

function turnLeft()
   turtle.turnLeft()
   turtle.detect()
   turtle.dig()
   turtle.attack()
   turtle.forward()
   turtle.turnLeft()
end

function getInput()
   print "Enter Rows : "
   rows = read()
   print "Enter Columns : "
   columns = read()
   print "Enter Height : "
   height = read()
end

getInput()

while height < 4 do
  for j = 0, columns do
	 for k = 0, rows do
		forward()
	 end
	 if turtle % 2 == 0 or turtle % 2 == 2 then
	   turnRight()
	end
	else
	   turnLeft()
	end
end
Edited on 15 May 2015 - 03:40 AM
Bomb Bloke #2
Posted 14 May 2015 - 03:46 AM
Your forward() function will fail when encountering stacks of sand/gravel. The usual way to deal with this is a loop:

local function forward()
	while not turtle.forward() do
		turtle.dig()
		turtle.attack()
	end
end

You could shrink your turning functions down by making use of your forward() function:

local function turnRight()
	turtle.turnRight()
	forward()
	turtle.turnRight()
end

Taking the modulus of "turtle" isn't going to work; I'm guessing you meant "j". You've also got a malformed "if" block in there - you can't "else" after you've "end"ed.

There's really no point in returning to the start in between layers - it's far more efficient to just go up and dig out the next one from where ever you end up.

Let's say there's an odd number of columns - on completing a layer, the turtle could go up one, turn around, and complete the exact same loop as it did before in order to dig out the next layer.

On the other hand, if there's an even number of layers, you'd do the same thing but invert the order of the lefts and rights at the end of each row.

So, let's say you created a loop using "i" as the counter to track height. You'd then simply check whether (j + i) % 2 == 0 to determine which way to turn!
Football52 #3
Posted 14 May 2015 - 03:58 AM
Ahhhh that does make a lot of sense. I could decide if restarting from the same position would be best or not. Now that I look at the code there are a lot of silly mistakes my bad this is something I came up with quick and haven't actually tested. I'll figure in the height factor and fix the errors and post it again and see what you think.

I won't actually be able to test this for a few days as I'm away from the computer but wanted to figure out this program
Football52 #4
Posted 15 May 2015 - 05:40 AM
Okay I have modified the code, and I have tested it since I have been home. It works somewhat to a degree. For a test run, I entered a 5 x 5 x 5 area to be mined by the turtle. What happens is that the turtle goes 7 blocks in the row, turns right, mines 7 blocks back, and then turns right ago and goes down the previous path it just did.

So this what the first layer looks like :
**

**

**

**

**

**

Obviously its suppose to be a 5 x 5 but instead is doing a 7 x 2.

The other problem I am having is when the turtle goes up for the next layer, it turns the opposite way and does a reflection of the previous 7 x 2 that was made 1 layer below

This is what I mean by that :
**

** (Layer 1)

**

**

**

**

**

**

**

** (Layer 2)

**

**

**

**

As you can see the turtle is just reflecting the image of what it dig before 1 layer up. I've gone through the code and I'm just stuck on the logic of how its suppose to turn so it actually does a
5 x 5 x 5.

The code I have is as follow :



for i = 0, height do
  for j = 0, columns do
	for k = 0, rows do
	  refuel()
	  forward()
	end
	if ( j + i ) % 2 == 0 then
	  if j % 2 == 0 or j % 2 == 2 then
		turnRight()
	 else
		turnLeft()
	 end
	 else
		if j % 2 == 0 or j % 2 == 2 then
		   turnLeft()
		else
		   turnRight()
		end
	 end
  end

  up()

end

The up function I have is just this :


function up()
  turtle.detectUp()
  turtle.digUp()
  turtle.attackUp()
  turtle.up()
  turtle.turnLeft()
  turtle.turnLeft()
end