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

[Turtle] Quarry Program Not Acting as Expected?

Started by Coolflip, 18 December 2012 - 08:11 PM
Coolflip #1
Posted 18 December 2012 - 09:11 PM
**SOLVED**

Hello, I have started picking up turtle programming lately as I have programmed java/c++ for a few years now, and have learned fairly quickly.

I have been trying to create a quarry program, but I cannot figure out where it is going wrong. The program is supposed to dig forward X blocks, turn right/left, move forward, then turn right/left again, then repeating the whole process. It runs the first section fine, but it is when it turns around that something goes wrong, moving forward an extra space before needing to turn around again. I have tried printing out numbers of the for loop to figure out if it was starting under a different value, but to no success.

By the way, I know that as of now the program will not start digging down properly. I am trying to get one layer perfect before I worry about the rest.

Also, if someone could clarify how remainders (X%2=0 for example) work with turtles that would be awesome. You can see where it would come in handy with my side=0, side=2, side=4, etc.

Pictures of the problem:








Program code:





local tArgs = { ... }
local length = tonumber(tArgs[1])
local width = tonumber(tArgs[2])
local depth = tonumber(tArgs[3])
local down=0
local side=0

-- Functions --
function fuel()
  if turtle.getFuelLevel() < 5 then
	turtle.select(16)
	turtle.refuel(1)
	turtle.select(1)
  end
end

-- Program --
turtle.forward()
fuel()

while down<=depth do
  repeat
	repeat
	  for i=1, length do
		turtle.digDown()
		turtle.forward()
		write(i)
	  end
	  i=0
	  fuel()
	  if side==0 or side==2 or side==4 or side==6 or side==8 or side==10 then
		turtle.turnRight()
		turtle.forward()
		turtle.turnRight()
	  else
		turtle.turnLeft()
		turtle.forward()
		turtle.turnLeft()
	  end
	  side=side+1
	until side==length
	side=0
	down=down+1
  until down==depth
end


theoriginalbit #2
Posted 18 December 2012 - 09:42 PM
% is a modulo (or modulus) function. how it works is it divides the numbers and returns the remainder. so 4%2 would = 0 but 2%4 would = 2 since 4 goes into 2, 0 times with a remainder of 2.

to use this in a program you would say do this

if ( facing % 4 == 0 ) then -- where facing is a number variable somewhere in your code
makes sense?
Coolflip #3
Posted 18 December 2012 - 09:59 PM
Yeah I understood how the % sign works as I've programmed with java/c++ before, but thanks for clarifying how it is used in LUA. I just couldn't get it to work with an if statement before but I didn't have the parentheses.

Still looking on explanation to my quarry problems though :/
theoriginalbit #4
Posted 18 December 2012 - 10:03 PM
but I didn't have the parentheses.

shouldn't be needed. thats just my better programmer coming out in me, the one that codes to standards. :P/>

i normally use % to reset variables such as

facing = facing + 1 % 4


looking into code now.
theoriginalbit #5
Posted 18 December 2012 - 10:16 PM
i think this is a problem


if side == 0 or side == 2 or side == 4 or side == 6 or side == 8 or side == 10 then

just use a boolean and toggle it each loop.
theoriginalbit #6
Posted 18 December 2012 - 10:17 PM
also just incase you didn't know, "excavate" is a program thats on the turtles that do the same function as this program does.
remiX #7
Posted 18 December 2012 - 10:49 PM
TheOriginalBit, stop multi-posting all the time and edit your posts. You've done it so many times
theoriginalbit #8
Posted 18 December 2012 - 10:55 PM
TheOriginalBit, stop multi-posting all the time and edit your posts. You've done it so many times

Well at least I'm helping them remiX. all you seem to do is tell me to stop doing stuff or that I'm wrong.
Coolflip #9
Posted 18 December 2012 - 11:01 PM
I figured out the only problem was with the for statement


for i=1, length do
turtle.digDown()
turtle.forward()
end

It just needed to be length-1, but it seemed to work on the first pass because there was a wall preventing it from moving it one space further.

Thanks for your help!
remiX #10
Posted 18 December 2012 - 11:31 PM
When I tested your program, I made the depth 2 but it didn't go down so I redid your program:

Spoiler
local tArgs = { ... }
local length = tonumber(tArgs[1])
local width = tonumber(tArgs[2])
local depth = tonumber(tArgs[3])
if #tArgs ~- 3 or not length or not width or not depth then
    print("Usage: " .. fs.getName(shell.getRunningProgram()) .. " <length> <width> <depth>")
    return
end
-- Variables
right = true
x = false
digF = turtle.dig
digU = turtle.digUp
digD = turtle.digDown
moveF = turtle.forward
moveR = turtle.turnRight
moveL = turtle.turnLeft
moveB = turtle.back
moveU = turtle.up
moveD = turtle.down
place = turtle.placeDown

-- Functions --
function fuel()
  if turtle.getFuelLevel() < 5 then
	    turtle.select(16)
	    turtle.refuel(1)
	    turtle.select(1)
  end
end

-- Program --
turtle.forward()
fuel()

for i = 1, depth do
    for k = 1, width do
        for j = 1, length do
            digD()
            if j ~= length then    moveF() end
        end
        if k == width then break end
        if right and not x then
            moveR()
            moveF()
            moveR()
            right = not right
        elseif right and x then
            moveL()
            moveF()
            moveL()
            right = not right
        elseif not right and not x then
            moveL()
            moveF()
            moveL()
            right = not right
        elseif not right and x then
            moveR()
            moveF()
            moveR()
            right = not right
        end
    end
    right = not right
    x = not x
    moveD()
    moveR()
    moveR()
end

-- Return to start
for i = 1, depth do moveU() end
for i = 1, width do moveF() end
moveR()
for i = 1, length - 1 do moveF() end
moveR()

term.clear()
term.setCursorPos(1,1)
print("Cleared a " .. length .. "x" .. width .. "x" .. depth .. " hole.")

Used variables for it to know what to do when to do it, ie: right or left, and if it has gone down or not.