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

Inventory Check Loop?

Started by aarondm, 04 January 2013 - 02:14 AM
aarondm #1
Posted 04 January 2013 - 03:14 AM
Hey guys, quick question.

I'm trying to figure out how to code my Redwood Tree program so it will loop indefinitely so long as it has the required supplies.

I'm not sure what code to use to make a repeating loop. I'm also not sure how to implement a "pick-up" so it will continue doing what it was doing if I logoff/logon. Is this a difficult thing to implement into a program?

I'm assuming that I could create the loop and if all ifs returned true, run the program, then check after each run. Any suggestions? I currently have it saved as a startup and threw a reboot at the end. This doesn't solve the problem because it glitches if any one item runs out.

My code:
Spoiler
--Basic fuel check function
function checkFuel()
  if turtle.getFuelLevel() < 100 then
	repeat
	turtle.select(16)
turtle.refuel(2)
turtle.select(1)
	until turtle.getFuelLevel() >= 100
  end --if
end --checkFuel()

--Plants 4 Redwood saplings in a 2x2
function plantTree()
turtle.select(14)
turtle.forward()
turtle.place()
turtle.back()
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.place()
turtle.back()
turtle.place()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
end --plantTree()

--Applies bonemeal and checks if tree has grown. If not, sleeps and repeats.
function growTree()
turtle.select(15)
turtle.place()
turtle.up()
  if turtle.forward() == true then
	repeat
turtle.back()
turtle.down()
sleep(120)
turtle.place()
turtle.up()
	until turtle.forward() == false
end --if
turtle.down()
end --growTree()

--Cuts down the Redwood. Up the left side, then down the right side.
function cutTree()
turtle.dig()
sleep(0.25)
turtle.forward()
turtle.dig()
  local height = 0
  if turtle.detectUp() == true then
	repeat
turtle.dig()
turtle.digUp()
turtle.up()
height = height + 1
	until turtle.detectUp() == false
end --if
checkFuel()
turtle.turnRight()
  if turtle.detect() == true then
turtle.dig()
end --if
turtle.forward()
turtle.turnLeft()
  if turtle.detectDown() == true then
	repeat
turtle.dig()
turtle.digDown()
turtle.down()
height = height - 1
	until height == 0
end --if
turtle.dig()
end --cutTree()

--Deposits the first 7 slots in a chest behind the turtle.
function dropOff()
checkFuel()
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.select(1)
turtle.drop()
turtle.select(2)
turtle.drop()
turtle.select(3)
turtle.drop()
turtle.select(4)
turtle.drop()
turtle.select(5)
turtle.drop()
turtle.select(6)
turtle.drop()
turtle.select(7)
turtle.drop()
turtle.select(1)
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
checkFuel()
end --dropOff()

--Attempts to light leftover leaves on fire, then returns to original position.
function burnTree()
  if turtle.detectUp() == false then
	repeat
turtle.up()
	until turtle.detectUp() == true
end
turtle.down()
turtle.select(13)
turtle.placeUp()
  if turtle.detectDown() == false then
	repeat
turtle.down()
	until turtle.detectDown() == true
end
turtle.select(1)
end --burnTree()

--Main Script
--PLACE checkInventory() function here.
checkFuel()
plantTree()
growTree()
checkFuel()
cutTree()
checkFuel()
dropOff()
burnTree()
shell.run("reboot")

Inventory is 13-16 and is Flint/tender, saplings, bonemeal, and fuel.
PixelToast #2
Posted 04 January 2013 - 03:17 AM
make a function that uses turtle.getItemCount(slotNum)
then make a conditional while loop around your main code
aarondm #3
Posted 04 January 2013 - 03:22 AM
make a function that uses turtle.getItemCount(slotNum)
then make a conditional while loop around your main code

That's the function I thought would be used but how would you implement that into a while loop?


while turtle.getItemCount(13) >= 1 do
while turtle.getItemCount(14) >= 4 do
while turtle.getItemCount(15) >= 2 do
while turtle.getItemCount(16) >= 2 do
    Insert Tree function here
end

Would you nest the while conditions within each other like that? Also, what would you use to cause the while to loop?
PixelToast #4
Posted 04 January 2013 - 03:24 AM
ohh
you can combine expressions like this:
while turtle.getItemCount(13) >= 1 and turtle.getItemCount(14) >= 4 and turtle.getItemCount(15) >= 2 and turtle.getItemCount(16) >= 2 do
--code
end
aarondm #5
Posted 04 January 2013 - 03:29 AM
ohh
you can combine expressions like this:
while turtle.getItemCount(13) >= 1 and turtle.getItemCount(14) >= 4 and turtle.getItemCount(15) >= 2 and turtle.getItemCount(16) >= 2 do
--code
end

So could I use something like this:


function.inventoryCheck()
while turtle.getItemCount(13) >= 1 and turtle.getItemCount(14) >= 4 and turtle.getItemCount(15) >= 2 and turtle.getItemCount(16) >= 2 do
    return true    
end -- checkInventory()
while checkInventory == true do
    repeat
    INSERT Tree cutting program
    until inventoryCheck == false
end

Would this work? I'm not sure the proper syntax to use for this process.
PixelToast #6
Posted 04 January 2013 - 03:32 AM
this would be better:

function.inventoryCheck()
return turtle.getItemCount(13) >= 1 and turtle.getItemCount(14) >= 4 and turtle.getItemCount(15) >= 2 and turtle.getItemCount(16) >= 2
end
while inventoryCheck() do
--do stuff
end
aarondm #7
Posted 04 January 2013 - 03:34 AM
this would be better:

function.inventoryCheck()
return turtle.getItemCount(13) >= 1 and turtle.getItemCount(14) >= 4 and turtle.getItemCount(15) >= 2 and turtle.getItemCount(16) >= 2
end
while inventoryCheck() do
--do stuff
end

Awesome! I knew it couldn't be that hard. I just didn't know how to implement it. Is persistence hard to code into a program? I'd like the program to not completely lose its mind when I log out.
PixelToast #8
Posted 04 January 2013 - 03:47 AM
theres not verry many ways to do that :|
its better to just make sure the program has stopped before you logged out
if your on a server though, you can use world anchors
aarondm #9
Posted 04 January 2013 - 03:50 AM
theres not verry many ways to do that :|
its better to just make sure the program has stopped before you logged out
if your on a server though, you can use world anchors

I figured it might be a pain. I see some of the advanced programs flaunt that feature. It seemed cool.

How would I program a numerical input for how many times to run the program on top of all this while loop stuff.
PixelToast #10
Posted 04 January 2013 - 03:58 AM
replace the while loop with this:

write("Times: ")
times=tonumber(read())
for i=1,times do
if not inventoryCheck() then
break
end
-- do stuff
end

you can also just count and compare but i prefer using break
aarondm #11
Posted 04 January 2013 - 03:59 AM
replace the while loop with this:

write("Times: ")
times=tonumber(read())
for i=1,times do
if not inventoryCheck() then
break
end
-- do stuff
end

you can also just count and compare but i prefer using break

Thank you so much!

So, "break" just ends a program there and goes back to the OS?
PixelToast #12
Posted 04 January 2013 - 04:03 AM
break will just end the innermost loop (in this case the FOR loop)
if you want to exit out of the program you can use return or error()
aarondm #13
Posted 04 January 2013 - 04:06 AM
break will just end the innermost loop (in this case the FOR loop)
if you want to exit out of the program you can use return or error()

Oh ok. I get it. Thanks!