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

Bridge turtle doesn't know when inventory is empty...

Started by circularlogic, 11 November 2012 - 08:54 AM
circularlogic #1
Posted 11 November 2012 - 09:54 AM
Hello! I am completely new to ComputerCraft and LUA (just started messing with LUA today!)

I am running the most current Tekkit server and client, which means I am using CC 1.33.

My bridgebot moves forward and mines or lays blocks as needed to create a tunnel/catwalk through the upper blocks of the nether.

My bridge-building bot doesn't seem to recognize when he is out of materials. I tried to fix that by cobbling some found code together here and other places, but I am just too new to the language to know what I am doing wrong.

When I run the code with the "checkslot" inventory detector, the turtle does nothing. Without it, and with a simple "repeat X times" appended to the bottom, the rest of the code works as intended (accept it simply keeps going until X times regardless of whether is has the materials to use…




local i = 1
local
slotNum = 1

function checkslot()
if turtle.getItemCount(slotNum) < 4 then
	if slotNum == 6 then  
		return false
	end
  
	for i = slotNum+1, 6 do  
		if turtle.getItemCount > 4 then	
			slotNum = i	
			turtle.select(slotNum)	
		return true  
		end
	end
end
end



function bridgechunk()
turtle.turnLeft()	
	if not turtle.detect() then		
	checkslot()		
	turtle.place()	
	end
  
turtle.turnLeft()
turtle.turnLeft()	
	if not turtle.detect() then		
	checkslot()		
	turtle.place()	
	end
turtle.turnLeft()
end





function spinmine()
if not turtle.forward() then	
	turtle.dig()	
	turtle.forward()	
	turtle.digUp()	
	bridgechunk()
end
  
	if not turtle.detectDown() then	
	turtle.placeDown()	
	bridgechunk()
	end
end



while checkslot()==true do
spinmine()
print ("Let's Roll!")
end

  

I suspect I am simply not properly calling my functions, or doing it in a backwards way. I simply want the turtle to:

1. check his selected slot before placing a brick.
2. if that slot has less than 4 items remaining in it, then switch to the next slot.
3. If, after depleting what is in slots 1-5 - and he has selected slot 6 - AND it has less than 4 items, stop the program.

Can anyone lend a little instruction?

Thank you, guys. And it is a fantastic mod, by the way!
Doyle3694 #2
Posted 11 November 2012 - 10:18 AM
I see some bugs. first, you spinmine function calls bridechunk twice if both conditions are met. secondly, you dont need to use == true because while is really looking for a true bool, which will be put there if your function is called. Though I do not see why it doesnt detect material depletion. Though i can see that your for-loop should be
for i=slotnum,6 do
I think it elevates through your slots?
circularlogic #3
Posted 11 November 2012 - 11:24 AM
I see some bugs. first, you spinmine function calls bridechunk twice if both conditions are met. secondly, you dont need to use == true because while is really looking for a true bool, which will be put there if your function is called. Though I do not see why it doesnt detect material depletion. Though i can see that your for-loop should be
for i=slotnum,6 do
I think it elevates through your slots?

Thank you for the reply. I made the changes I think you suggested, and the bot still sits there doing nothing. It has to be an issue with the Function checkslot() since the rest of the code works find with a simple Repeat at the bottom.

Also, now that I changed my for-loop to


for i=slotnum,6 do

How will it know to sequentially advance through the slots as they are depleted?

here's my code with the changes.



local i = 1
local
slotNum = 1

function checkslot()
turtle.select(1) 
if turtle.getItemCount(slotNum) < 4 then   
    if slotNum == 6 then	 
        return false
    end   
    for i=slotNum,6 do	 
        if turtle.getItemCount(slotNum) > 4 then	   
            slotNum = i	   
            turtle.select(slotNum)	   
        return true	 
        end  
    end   
end
end

function bridgechunk()   
turtle.turnLeft()       
    if not turtle.detect() then           
    checkslot()           
    turtle.place()       
    end   
turtle.turnLeft()   
turtle.turnLeft()       
    if not turtle.detect() then           
    checkslot()           
    turtle.place()       
    end   
turtle.turnLeft()
end

function spinmine()   
if not turtle.forward() then       
    turtle.dig()       
    turtle.forward()       
    turtle.digUp()       
    bridgechunk()   
end
   
    if not turtle.detectDown() then       
    turtle.placeDown()       
    bridgechunk()   
    end   
end

while checkslot() do
spinmine()
print("Let's Roll!")
end
 
Doyle3694 #4
Posted 11 November 2012 - 11:27 AM
its less efficient, but it would work better. though now I see that if statement at the beggining(unindented code tends to hide)
Doyle3694 #5
Posted 11 November 2012 - 11:33 AM
OOOOH. at the checkslot function, add

return true
at the end of the function, so the function will be:

function checkslot()
   if turtle.getItemCount(slotNum) < 4 then
		   if slotNum == 6 then
		      return false
		   end

		   for i = slotNum+1, 6 do
			    if turtle.getItemCount > 4 then
					 slotNum = i	
					 turtle.select(slotNum)
				     return true
			    end
		   end
   end
   return true
end
circularlogic #6
Posted 11 November 2012 - 12:07 PM
BRIDGE BOT NOW READY FOR SERVICE!

Bastard shot right through the side of my testing facility and ran until he was out of cobble. Just like I wanted him to do, and just like you fixed him to do.

Thank you very, very much.

And I have one more question:

You mentioned my code was not indented. I thought I was doing it right, so sorry! Do I indent before AND After every IF statement, as well? Is that what I did wrong, or it there some other synthax I need to follow as well?

I guess I am asking: Are there some simple rules a noob like me can remember regarding indentation?

Thanks again!

EDIT: Here's my finished code for posterity. I also cleaned it up a little between posts:



local i = 1
local
slotNum = 1

function checkslot()
   if turtle.getItemCount(slotNum) < 4 then
				   if slotNum == 6 then
					  return false
				   end

				   for i = slotNum+1, 6 do
						    if turtle.getItemCount > 4 then
										 slotNum = i    
										 turtle.select(slotNum)
									 return true
						    end
				   end
   end
   return true
end

function spinmine()
sleep(.5)
if not turtle.forward() then        
    turtle.dig()        
    turtle.forward()        
    turtle.digUp()        
    bridgechunk()    
end
end

function bridgechunk()
turtle.placeDown()    
turtle.turnLeft()        
    if not turtle.detect() then            
        checkslot()            
        turtle.place()        
    end    
turtle.turnLeft()    
turtle.turnLeft()        
    if not turtle.detect() then            
        checkslot()            
        turtle.place()        
    end    
turtle.turnLeft()
end


function gotime()
if not turtle.forward() then
    spinmine()
end
if not turtle.detectDown() then
    bridgechunk()
end
end

while checkslot() do
gotime()
end

Doyle3694 #7
Posted 11 November 2012 - 12:14 PM
you was not indenting after the function declaring :unsure:/>/>
circularlogic #8
Posted 12 November 2012 - 03:03 AM
Sigh, I guess I spoke too soon. The mining turtle does build his bridge like he is supposed to, but he doesn't stop when his inventory is empty and appears to use each 1-6 slot number's items to build his bridge even when they are <4.
Doyle3694 #9
Posted 12 November 2012 - 03:25 AM
you should get a bug, because

  if turtle.getItemCount > 4 then
	   slotNum = i  
	   turtle.select(slotNum)
	   return true
   end
should be:

  if turtle.getItemCount(i) > 4 then
	 slotNum = i  
	 turtle.select(slotNum)
	 return true
  end

EDIT: remove 'turtle.select(1)' at the beggining of checkslot
circularlogic #10
Posted 15 November 2012 - 04:06 AM
Thank you, by the way. This is working great, now.
oasis9 #11
Posted 16 November 2012 - 07:35 PM
I want my turtle to check that every last drop of items are gone from its inventory, before it does ' shell.run("return") '!!!!!
HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
oasis9 #12
Posted 16 November 2012 - 07:41 PM
(Removed)
oasis9 #13
Posted 16 November 2012 - 07:43 PM
My pic/info on this wiki says "clueless"!!! *_*
I am not clueless!

Plus, I am pretty good at code!
oasis9 #14
Posted 16 November 2012 - 07:52 PM
Man I don't like this turtle!!
remiX #15
Posted 17 November 2012 - 01:34 AM
oasis9, instead of spamming quad posts make your own thread whereby you paste your problematic code and state what your problem is.