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

Strip mining program - need help with turning

Started by dragon40226, 20 January 2014 - 09:01 AM
dragon40226 #1
Posted 20 January 2014 - 10:01 AM
Hello this is my first post on this site, i worked on this program for a good 30-60 minutes and couldn't get it working.

What i am trying to do is get it to mine however many blocks the user wants, then ask him how many different tunnels to make.

The problem i have is that i cant get it to switch which way it needs to turn, it needs to mine straight then go right, then mine straight then go left. The way I tried to make this is with the increment operation and to decide if the number is positive then to turn right, and if its not turn left.

Any other ideas for how i could do this?

Code:

print("How long do you want to tunnel?")
local length = read()
print("Okay and how many tunnels?")
local n = read()
print("Going: ", length, " long with ", n, " different tunnels")
x = length
c=0
local function tryRefuel()
for i = 1,16 do
  if turtle.getItemCount(i) > 0 then
   turtle.select(i)
   if turtle.refuel(1) then
    return true
   end
  end
end
turtle.select(1)
return false
end
local function refuel()
	    curFuel = turtle.getFuelLevel()
	    if curFuel == "unlimited" or curFuel >= 10 then
			    return
	    else
			    if not tryRefuel() then
					    print("Please supply more fuel to coninue...")
					    while not tryRefuel() do
							    sleep(1)
					    end
			    end
	    end
end
function mod(a, B)/>
return a - (math.floor(a/B)/>)
end
function tunnel()
for i=0,x do
  refuel()
  turtle.dig()
  turtle.digUp()
  turtle.digDown()
  turtle.forward()
end
end
function loop()
local c = 0
for a=0,n do
  tunnel()
  if(mod(c,2) == 0) then
   turtle.turnRight()
  
   turtle.dig()
   turtle.digUp()
   turtle.digDown()
   turtle.forward()
  
   turtle.dig()
   turtle.digUp()
   turtle.digDown()
   turtle.forward()
  
   turtle.dig()
   turtle.digUp()
   turtle.digDown()
   turtle.forward()
  
   turtle.turnRight()
  end
  if(mod(c,2) == 1) then
   turtle.turnLeft()
  
   turtle.dig()
   turtle.digUp()
   turtle.digDown()
   turtle.forward()
  
   turtle.dig()
   turtle.digUp()
   turtle.digDown()
   turtle.forward()
  
   turtle.dig()
   turtle.digUp()
   turtle.digDown()
   turtle.forward()
  
   turtle.turnLeft()
  end
c = c + 1
end
end
loop()
CometWolf #2
Posted 20 January 2014 - 11:51 AM
as far as i can tell, this

function mod(a, B)/>/>/>/>
return a - (math.floor(a/B)/>/>/>/>)
end
will most likely result in 1 or more as long as you use 2 as B, which you do. I do however have no idea what the "/>" is for.
Anyways, a quick and easy way to alternate your loop is to use the modulo operaor "%".

function even(n)
  return (n%2 == 0)
end
This function will return true if the number given is an even number, and false if it's an odd number. Thus 1 will return false, and 2 will return true.
I have no idea how to explain the modulo operation lol, but if you want you can read about it here
http://en.wikipedia....odulo_operation
Edited on 20 January 2014 - 04:30 PM
dragon40226 #3
Posted 20 January 2014 - 11:54 AM
im sorry yeah a even number or a odd number, not a positive or negative, and is the modulus operator possible in lua? i came from JAVA and i didnt think that it was in LUA
OReezy #4
Posted 20 January 2014 - 04:55 PM
It returns whatever is left over after even division. For example:
10/7 = 1 (3/7) so modulo gives you 3
print(10%7) -->3
Lots of people know it by remainder.
surferpup #5
Posted 22 January 2014 - 10:21 AM
Take a look at your function loop(). I broke it into a couple of functions for you. It should do what you are after.

A new function doDigging() which performs your digging and moves forward.


function doDigging()
   turtle.dig()
   turtle.digUp()
   turtle.digDown()
   turtle.forward()
end

This is nice, because you can also simplify your tunnel() function to read as follows:


function tunnel()
  for i=0,x do
	refuel()
	doDigging()
  end
end

A new function makeTurn(arg) which takes an integer argument. If arg is even, the turtle turns right. If odd, the turtle turns left.


function makeTurn(arg)
  if arg%2 == 0 -- arg is even
	turtle.turnRight()
  else -- arg is odd
	turtle.turnLeft()
  end
end

I rewrote your loop function. It now takes an integer argument (howManyTimes) which will then perform the loop that many times. Since you are zero basing your for loop (you start at zero), I terminate the loop at howManyTimes - 1. If you actually wanted to go n+1 times (which is what you are doing in your code by starting the for loop at zero), you will need to get rid of the -1 here.


function loop(howManyTimes)
  for thisDig=0,howManyTimes-1 do
	tunnel()
	makeTurn(thisDig)
	doDigging()
	doDigging()
	doDigging()
	makeTurn(thisDig)
  end
end

This should be functionally equivalent to what you were after. You could easily place the three calls to doDigging() within a for loop, but it really doesn't matter. If you were going to do it more than three times, I would place them in a for loop.

The last line of your code (where you call loop() ) should now read


loop(n)

The problem you are having in debugging is that your code is all over the place. By breaking it down into meaningful functions first, and then creating the execution block of code at the end, it is much easier to troubleshoot, and you can re-use things when you need to.

Here is your entire code, re-organized and with the changes I recommended

-- Moving / Digging Functions

function doDigging()
   turtle.dig()
   turtle.digUp()
   turtle.digDown()
   turtle.forward()
end

function makeTurn(arg)
  if arg%2 == 0 -- arg is even
	turtle.turnRight()
  else -- arg is odd
	turtle.turnLeft()
  end
end

function tunnel()
  for i=0,x do
	refuel()
	doDigging()
  end
end

function loop(howManyTimes)
  for thisDig=0,howManyTimes-1 do
	tunnel()
	makeTurn(thisDig)
	doDigging()
	doDigging()
	doDigging()
	makeTurn(thisDig)
  end
end

--Refueling Functions

function tryRefuel()
  for i = 1,16 do
	if turtle.getItemCount(i) > 0 then
	 turtle.select(i)
	 if turtle.refuel(1) then
		return true
	 end
	end
  end
  turtle.select(1)
  return false
end

function refuel()
  curFuel = turtle.getFuelLevel()
  if curFuel == "unlimited" or curFuel >= 10 then
	return
  else
	if not tryRefuel() then
		print("Please supply more fuel to coninue...")
		while not tryRefuel() do
			sleep(1)
		 end
	end
  end
end

-- Program Begins Here

print("How long do you want to tunnel?")
local length = read()
print("Okay and how many tunnels?")
local n = read()
print("Going: ", length, " long with ", n, " different tunnels")
x = length
loop(n)

Let me know how that works for you.
Edited on 22 January 2014 - 09:56 AM