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

[Q] Code not running correct function

Started by Mrkalo981, 30 December 2012 - 07:28 AM
Mrkalo981 #1
Posted 30 December 2012 - 08:28 AM
Hi everyone. I have recently started programming for Computercraft and I am trying to write my own auto quarry (with custom dimensions) that dumps items in a chest when its inventory gets full. Here is the code below. It is quite messy right now, I am still testing it.

X = tonumber(read())
Y = tonumber(read())
Z = tonumber(read())
B = 0 -- Used to calcualte the turtles position.
C = 0 -- Used to calculate the turtles position.
D = 0 -- Used to calculate the turtles position.
A = 2
X = X - 1
Z = Z - 1

function mine()

while turtle.forward() == false do
if turtle.dig() == false then
turtle.attack()
end
end

end

function lineforward()
for num=1, X do
   mine()
	 B = B + 1
end

end

function linebackward()
for num=1, X do
   mine()
	  B = B - 1
end

end

function calculate()

if math.fmod(A, 2) == 0 then  --This part of the code does not run the extra time with an odd Z value.
lineforward()
   turtle.turnLeft()
	 mine()
	  turtle.turnLeft()
			D = D + 1
else linebackward()
	   turtle.turnRight()
		  mine()
			   turtle.turnRight()
					  D = D + 1
end

A = A + 1

end

function level()

turtle.dig()
  turtle.refuel(1)
	turtle.forward()
	   turtle.turnRight()


for num=1, Z do
	calculate()
end

end

level()

The code runs fine when I have an even Z value however when Z is odd (for example 3) it only runs an even number of times in function calculate (the part in bold). When I set Z to 3 that code should run twice and the else code once, however it does not run the second time. Please help me!
ChunLing #2
Posted 30 December 2012 - 08:48 PM
Um…what's this bit about "Z = Z - 1"

Are you taking that into account, that when you set Z to 3, you reset it to 2? And when you set Z to, say, 4, is the turtle going back and forth twice?
Mrkalo981 #3
Posted 30 December 2012 - 09:18 PM
Problem is if I take it out then it goes back and forth 4 times rather than 3. It does not seem to be able to stop on an odd number of line functions.

EDIT: Where I have my if then else math.fmod statement it seems to always run both parts and I do not know how to fix it.
ChunLing #4
Posted 01 January 2013 - 12:20 AM
Huh…confirm that by adding a couple of debug prints in there. Cause that really doesn't seem likely.