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

Mining turtle doesn't see the catch

Started by MentolDP, 21 June 2013 - 02:57 PM
MentolDP #1
Posted 21 June 2013 - 04:57 PM
Hello, my mining turtle digs on the x axis until it reaches the distance set then turns left and doesn another etc..
Now my problem is when it reaches the set distance for Z which is the amount of rows, it just keeps digging more
rows! I have a check in place to see if the curZ == setZbut the turtle seems to ignore it and keep going…I'm unsure
on how to proceed with this piece of code.

local distY = 0
local distX = 0
local distZ
local fuel = 0
local fuelNeeded = 0
local curFuel = 0

function boot()
	local turtleName
	if os.getComputerLabel ~= "Mining" then
		os.setComputerLabel("Mining")
		else
	end
	size()
	mining()
end
function size()
	print("How big do you want the hole?")
	write("Enter width: ")
	distX = tonumber(read())
	write("Enter length: ")
	distZ = tonumber(read())
	write("Enter depth: ")
	distY = tonumber(read())
	check()
end
function check()
	local choice
	write("You chose a size that was " ..distX.. " x " ..distZ.. " x " ..distY.. " big. Do you wish to change your choice? Y/N ")
	choice = read()
	if choice == "Y" then
		size()
		else
	end
	fuelCalc()
end

function invCheck()

end
function fuelCalc()
	fuelNeeded = (distX*distZ*distY)+(distY)
	fuel = math.ceil(80/fuelNeeded)
end
function mining()
	local curDistX = 1
	local curDistY = 0
	local curDistZ = 1
	curFuel = turtle.getFuelLevel()
	while curFuel < fuelNeeded do
		print("Not enough fuel, add " ..fuel.. " fuel units to the last slot.")
		turtle.select(16)
		turtle.refuel()
		curFuel = turtle.getFuelLevel()
		sleep(2)
	end
	turtle.select(1)
--mostly here
	while curDistY ~= distY do
		while curDistZ <= distZ do
			if curDistZ == distZ then
				if distZ % 2 == 0 then
					turtle.turnRight()
					for i=1,distZ do
						turtle.forward()
					end
					turtle.turnRight()
					print("I go back to the start!")
					curDistZ = 0
					else
					turtle.turnLeft()
					for j=1,distZ do
						turtle.forward()
					end
					turtle.turnLeft()
					print("I go back to the start!")
					curDistZ = 0
				end
				turtle.digDown()
				turtle.down()
				print("I went down!")
				curDistZ = 1
				curDistY = curDistY + 1
			end
--to here
			while curDistX ~= distX do
				turtle.dig()
				turtle.forward()
				print("I went forward!")
				curDistX = curDistX + 1
				if curDistX == distX then
					if curDistZ % 2 == 0 then
						turtle.turnRight()
						turtle.dig()
						turtle.forward()
						turtle.turnRight()
						print("I turned right!")
						curDistX = 1
						curDistZ = curDistZ + 1
						else
						turtle.turnLeft()
						turtle.dig()
						turtle.forward()
						turtle.turnLeft()
						print("I turned left!")
						curDistX = 1
						curDistZ = curDistZ + 1
					end
					else
				end
			end
		end
	end
end

boot()

Lyqyd #2
Posted 22 June 2013 - 03:27 PM
Split into new topic.
ChunLing #3
Posted 22 June 2013 - 08:30 PM
Use for loops for this kind of thing.
Bomb Bloke #4
Posted 22 June 2013 - 08:34 PM
Let's prune out everything that doesn't modify curDistZ:

--mostly here
        while curDistY ~= distY do
                while curDistZ <= distZ do
                        if curDistZ == distZ then
                                if distZ % 2 == 0 then
                                        curDistZ = 0
                                        curDistZ = 0
                                end
                                curDistZ = 1
                        end
--to here
                        while curDistX ~= distX do
                                if curDistX == distX then
                                        if curDistZ % 2 == 0 then
                                                curDistZ = curDistZ + 1
                                                curDistZ = curDistZ + 1
                                        end
                                        else
                                end
                        end
                end
        end
end

This should make your problem clearer to you. The moment curDistZ gets up to equal value of distZ, you're bringing it to 0 twice (sometimes), then to 1 (always). Hence there is absolutely no way curDistZ will ever be more then distZ.

Also, having an "else" statement immediately followed up by an "end" statement is completely pointless (and in this case, probably program-breaking). If you don't need "else", don't use it:

if <condition> then
  -- Code here gets performed if the condition is true.
end

-- Code here always gets performed.

And if you do need "else", the correct usage is:

if <condition> then
  -- Code here gets performed if the condition is true.
else
  -- Code here gets performed if the condition is false.
end

-- Code here always gets performed.