Jump to content




Strip mining program - need help with turning


  • You cannot reply to this topic
4 replies to this topic

#1 dragon40226

  • New Members
  • 2 posts

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()


#2 CometWolf

  • Members
  • 1,283 posts

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 by CometWolf, 20 January 2014 - 05:30 PM.


#3 dragon40226

  • New Members
  • 2 posts

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

#4 OReezy

  • Members
  • 91 posts

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.

#5 surferpup

  • Members
  • 286 posts
  • LocationUnited States

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

Let me know how that works for you.

Edited by surferpup, 22 January 2014 - 10:56 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users