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

[lua] strip mining automation complication

Started by Frosthawk87, 12 January 2013 - 04:12 PM
Frosthawk87 #1
Posted 12 January 2013 - 05:12 PM
Oh how my brain hurts. I'm usually pretty good at coding, but I'm trying to figure out how best to modify and simplify my code. If anyone can help me out with this, I'll be your best friend for longer than forever.

My goal is to have my turtle:
- dig a 2 high, 1 wide tunnel 101 blocks long
- place a torch on the upper block (upper block so it doesn't stop the turtle on its return, and left or right side doesn't matter) on the 2nd block in, and every 11 blocks after that, with the 101st block being where the final torch is placed
- before breaking each block, check to see if it's diamond ore (a piece of diamond ore will be placed in slot 16), if it is, return back to the starting point (allowing me to get my fortune 3 pick and cash in on some diamonds)
- and upon completion of said tunnel, return to its start point, at which time I'll remove its contents, pick it up, and place it back down to begin the next tunnel

Here's my code so far:

d = 0
repeat
  turtle.select(16)
	if turtle.compare() == false then
	  turtle.dig()
	  turtle.forward()
	  d = d + 1
	end
	if turtle.compare() == true then
	  turtle.turnRight()
	  turtle.turnRight()
		repeat
		  turtle.forward()
		  d = d - 1
		until d == 0
	end
		if turtle.compareUp() == false then
		  turtle.digUp()
		  d = d + 1
		end
		if turtle.compareUp() == true then
		  turtle.turnRight()
	  turtle.turnRight()
		repeat
		  turtle.forward()
		  d = d - 1
		until d == 0
	end
until d == 202
turtle.turnRight()
turtle. turnRight()
repeat
  if d > 0 then
	turtle.forward()
	d = d - 1
  end
until d == 0

As you can see, I haven't yet implemented the torch placing script, and I'm sure what I've got there can be majorly simplified.

Please help me Obi-Wan, you're my only hope…or something to that effect, Lol.
theoriginalbit #2
Posted 12 January 2013 - 05:20 PM
to place torches you can use the modulo / modulus function… place something to the effect of this as the last thing to be done before the until


local distMoved = ( d - 1 ) -- d is current moved, -1 allows for the "second one in" requirement
if distMoved % 11 == 0 or d == 101 then -- if its the 11th block or we are at the end
  turtle.select( torchSlot )
  turtle.placeUp()
end
crazyguymgd #3
Posted 12 January 2013 - 05:40 PM
First, you don't need to increment d when you dig up because you don't actually move. Then your repeat until would then go up to 101 not 202. If you did increment d, when you turn around to go back, you'll be going twice as far which isn't ideal.

Second tip, instead of

if false then
  ...
end
if true then
  ...
end
you can simplify to

if true then
  ...
else
  ...
end

Then you can just add TheOiginalBITs suggestion and you'll have made some progress in the right direction.