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

[Lua][Question]How do I separate a function out of a bigger function? Digging Turtle.

Started by Srenyti, 06 January 2013 - 06:42 PM
Srenyti #1
Posted 06 January 2013 - 07:42 PM
Hello I'm on my 5th day of coding ever so please be kind, any advice is welcome. Anyways I'm trying to
program a turtle to mine "d" block lengths, return, and then move to the rigth "b" amount of branches reapting the dig length each time. Everything runs fine until I reach the function that I named nextbranch(), the problem is that the turtle will move one extra branch over and then stop is there a way I can stop this? I've tried to put if thens, if then elseif's, for do, while do and I just can't seem to get to not move one extra branch over.

As an example I would input d = 5, b = 3 so it would mine 5 blocks deep and 3 branches it works just fine except on the final 3rd branch it moves one more branch over but doesn't mine it.


term.clear()
term.setCursorPos(1,1)

repeat

	write("How deep should I dig? ")
	d = tonumber(read(d))
		while d < 1 do
			write("Input must be a number, how deep to dig? ")
		end
		
	write("How many branches should I dig? ")
	b = tonumber(read(B)/>/>/>/>)
		while d <1 do
			write("Input must be a number, how many branches to dig?")
		end	
		
	print("Acknowledged will dig " ..d.. " blocks deep, and " ..b.. " branches.")
	
	write("Is this correct? [y/n] ")
	
	correct = io.read()
	
	term.clear()
	term.setCursorPos(1,1)
	
		
until correct == "y"

term.clear()
term.setCursorPos(1,1)
	
while true do
	print("Please place torches in slot top left and cobblestone in top middle.")
	print("Press any key when ready to begin.")
	event = os.pullEvent() -- any key will continue process --
	break
end

print("Beginning operations.")

--[[Start Functions]]--
		
function safefwd()
	success = false
	while not success do
		success = turtle.forward()
		if not success then
			turtle.dig()
			sleep(0.5)
		end
	end
end


function dig()	  -- begining in the middle of a 3x3 hallway it will dig 5 blocks into a wall and place a torch --
	turtle.select(1)
	move = d + 1
	n = 0
	for i = 1,move do
	
		n=n+1
	
		if not turtle.detectDown() then
			turtle.select(2)
			turtle.placeDown()
		end
				
		safefwd()
		
				
		if not turtle.detectDown() then
			turtle.select(2)
			turtle.placeDown()
		end
		
		while turtle.detectUp() do
			turtle.digUp()
			sleep(0.5)
		end		
		
		if n==6 then
			turtle.turnLeft()
			turtle.up()
			turtle.dig()
			turtle.select(1)
			turtle.place()
			turtle.down()
			turtle.turnRight()
			n=0
		end
	end
end


function done()	  -- turns around and returns to start --
	move = d + 1
	turtle.turnLeft()
	turtle.turnLeft()
	for i = 1,move do
		safefwd()
	end	
end	


function dropinv()
	local firstItem,lastItem = 3,16		-- defines slots 3 through 16 on turtle --
	
	for i = firstItem, lastItem do		 -- drops item into chest in middle of hallway below it --
		turtle.select(i)
		turtle.dropDown()
	end
	
	turtle.select(1)
	
end


function nextbranch()		-- moves the turtle over to the next branch --
	turtle.turnRight()		
	
	for i = 1,4 do
		safefwd()
	end
	
	turtle.turnRight()
end


--[[End Functions]]--

m = 0
repeat
	dig()
	done()
	dropinv()
	if m==b then		 -- I'm having difficulties here with preventing one extra iteration of nextbranch()--
		error()
		nextbranch()
	end
	m = m + 1
until m == b
theoriginalbit #2
Posted 06 January 2013 - 09:33 PM
firstly just another bug that jumped out at me while quickly scrolling to the bottom… this:



while true do
        print("Please place torches in slot top left and cobblestone in top middle.")
        print("Press any key when ready to begin.")
        event = os.pullEvent() -- any key will continue process --
        break
end

will actually continue on ANY event, not just a key event change the line
event = os.pullEvent()
with
event = os.pullEvent("key")


now to the actual problem. change the if at the end to be

if m ~= b then
  nextBranch()
end

and don't have the error() in there, that will exit the program on the first loop through.
Srenyti #3
Posted 06 January 2013 - 10:27 PM
Hey thank you for taking the time to look over it, I've made the bug changes you suggested its very much appreciated. I'm still pretty clueless and just getting things to barely work out just deleting and retyping things , so thank you! As for the problem, it still manifests itself on the final loop even with the m not equal b change. To further help visualize the problem I've taken a few screenshots. I am at my wits end :blink:/>.



m = 0
repeat
	dig()
	done()
	dropinv()
	if m ~= b then
		nextbranch()					-- I'm having difficulties here with preventing one extra iteration of nextbranch()--
	end
	m = m + 1
until m == b


This is of the start position with a tunnel of 5 depth and 3 branches:


This is of where I would like the turtle to end:


But this is where it still ends up going:
theoriginalbit #4
Posted 06 January 2013 - 11:00 PM
ok those screenshots helped see the problem… thanx :)/>

ok so it looks like the issue could be with done() since the bug is when its heading home… so it seems to me that you should not have


move = d + 1
for i = 1, move do
but instead just

for i = 1, d do


also i noticed another thing that may not work as expected in your code :)/>



repeat
  write("How deep should I dig? ")
  while true do
    local input = read()
    write( "\n" )
    d = tonumber( input )
    if not d then
      write("Input must be a number")
    elseif d < 1 then
      write("Input must be a positive number")
    else
      break
    end

    write( ", how deep to dig? " )
  end

  -- do the rest here in a similar way
until correct:lower() == "y" or correct:lower() == "yes"
personally I like to have it where the user can enter 'y' or 'yes' and using lower means they can enter yEs and it would still work.
Srenyti #5
Posted 06 January 2013 - 11:44 PM
The move = d +1 part of the code I added, because originally I had written this for the turtle to sit flush against the wall. It wasn't until later that I tried to incorporate turtle.drop() to place blocks into the chests you see in the pictures. Doing that however required that I add one extra block to the variable that the user inputed (the part that you helped clear up in your latest very helpful post) and I didn't know how, so I tried to add an extra movement somewhere and ended up at that part of the code. I removed that piece from the code of done() but it still keeps doing the same thing. Here is an updated program with the suggested changes, also if I may on the 5th line down from the cut out you pasted what exactly does "\n" do?



term.clear()
term.setCursorPos(1,1)

repeat

	write("How deep should I dig? ")
	while true do
		local input = read()
		write( "\n")
		d = tonumber( input )
		if not d then
			write("Input must be a number.")
		elseif d < 1 then
			write("Input must be a positive number")
		else
			break
		end
	end
		
	write("How many branches should I dig? ")
	while true do
		local input = read()
		write( "\n")
		b = tonumber( input )
		if not b then
			write("Input must be a number.")
		elseif b < 1 then
			write("Input must be a positive number")
		else
			break
		end
	end
			
	print("Acknowledged will dig " ..d.. " blocks deep, and " ..b.. " branches.")
	
	write("Is this correct? [y/n] ")
	
	correct = io.read()
	
	term.clear()
	term.setCursorPos(1,1)

		
until correct:lower() == "y" or correct:lower() == "yes"

term.clear()
term.setCursorPos(1,1)
	
while true do
	print("Please place torches in slot top left and cobblestone in top middle.")
	print("Press any key when ready to begin.")
	event = os.pullEvent("key") -- any key will continue process --
	break
end

print("Beginning operations.")

--[[Start Functions]]--
		
function safefwd()
	success = false
	while not success do
		success = turtle.forward()
		if not success then
			turtle.dig()
			sleep(0.5)
		end
	end
end


function dig()	  -- begining in the middle of a 3x3 hallway it will dig 5 blocks into a wall and places a torch --
	turtle.select(1)
	move = d + 1
	n = 0
	for i = 1,move do
	
		n=n+1
	
		if not turtle.detectDown() then
			turtle.select(2)
			turtle.placeDown()
		end
				
		safefwd()
		
				
		if not turtle.detectDown() then
			turtle.select(2)
			turtle.placeDown()
		end
		
		while turtle.detectUp() do
			turtle.digUp()
			sleep(0.5)
		end		
		
		if n==6 then
			turtle.turnLeft()
			turtle.up()
			if turtle.detect() then
				turtle.dig()
				sleep(0.5)
				if not turtle.detect() then
					turtle.dig()
					sleep(0.5)
				end
			end
			turtle.select(1)
			turtle.place()
			turtle.down()
			turtle.turnRight()
			n=0
		end
	end
end


function done()	  -- turns around and returns to start --
	turtle.turnLeft()
	turtle.turnLeft()
	for i = 1,move do
		safefwd()
	end	
end	


function dropinv()
	local firstItem,lastItem = 3,16		-- defines slots 3 through 16 on turtle --
	
	for i = firstItem, lastItem do		 -- drops item into chest in middle of hallway below it --
		turtle.select(i)
		turtle.dropDown()
	end
	
	turtle.select(1)
	
end


function nextbranch()		-- moves the turtle over to the next branch --
	turtle.turnRight()		
	
	for i = 1,4 do
		safefwd()
	end
	
	turtle.turnRight()
end


--[[End Functions]]--

m = 0
repeat		
	dig()				       -- begining in the middle of a 3x3 hallway it will dig 5 blocks into a wall and place a torch --
	done()				    -- turns around after dig() and returns to start (the middle of the hallway) --
	dropinv()				-- drops inventory --
	if m ~= b then			
		nextbranch()	   -- moves turtle over to the next branch, however it is here that is being run an extra time --
	end
	m = m + 1
until m == b

Srenyti #6
Posted 08 January 2013 - 10:41 PM
If anybody is still curious after further tinkering and a help from a few friends I've figured out what the problem was, m = 1 not 0 and I had to add another If then statement for when m == b to make the last dig, return, and dropping of inventory. Thank again for your help!

Everything else remains the same only the below is slightly different:


--[[End Functions]]--

m = 1
repeat		
	dig()					 -- begining in the middle of a 3x3 hallway it will dig 5 blocks into a wall and place a torch --
	done()					-- turns around after dig() and returns to start (the middle of the hallway) --
	dropinv()				-- drops inventory --

	if m ~= b then			
		nextbranch()		-- moves turtle over to the next branch, however it is here that is being run an extra time --
	end

	m = m + 1

	if m == b then
		dig()
		done()
		dropinv()
	end
	
until m == b