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

Compare Loop Pause

Started by thesonofdarwin, 01 January 2013 - 05:43 AM
thesonofdarwin #1
Posted 01 January 2013 - 06:43 AM
Hello all,

I picked up Beginning Lua Programming this weekend and after getting through the first 2 chapters I decided to try my first program.

It's a fairly basic 3x3 tunnel mining program that:
  • places a torch every 4 blocks
  • handles gravel (except with torch placement, but I think I can fix that)
  • discards all cobblestone gathered during its journey (it does this at each torch placement)
  • refuels conservatively (automatically) - this assumes coal/charcoal as fuel, otherwise you'll overfuel. May add check in the future for fuel type, but I always use coal.
  • returns to exactly where it began.
Now I'm quite sure this could be written simpler or with fewer (or more) functions, but I think I'll pick that up as I learn more about the language. Please feel free to point out the errors if you'd like, though.

My particular question is regarding my cobblestone check&discard step. It creates a short pause while it performs the check and drops cobblestone. Is there a better way to speed up this check? I could always make it check less frequently. I understand it's overkill early on, but as your inventory fills with gems and ores the check is nice.

Code segment in question:

function dropcobble()
for i=4,16 do
turtle.select(i)
if turtle.compareTo(2) then
turtle.drop()
end
end
end

Remainder of code for those curious.
ChunLing #2
Posted 01 January 2013 - 07:03 AM
You can just do it more often, and only use one stack for cobble.
function dropcobble()
    local stackSize = turtle.getItemCount(1) --or whatever your cobblestack is
    if stackSize > 32 then
        stackSize = stackSize-1
        turtle.select(1)
        turtle.drop(stackSize)
    end
end
Just do that whenever you might be getting too much cobble.
thesonofdarwin #3
Posted 01 January 2013 - 07:17 AM
The problem I had with doing something similar, though nowhere near as concise, was that the turtle inventory doesn't seem to be consistent in that it always finishes a stack of an item before it starts placing it in other slots. I could get around this by comparing item slots and moving items around, but then I run into the same problem of having to run checks.

I'm using CC1.4.6 if that matters.
ChunLing #4
Posted 01 January 2013 - 07:29 AM
When you dig or suck, the selected slot receives the inventory if possible. If not, then the "next available" slot gets it. This is a little inconsistent, but if you keep your cobblestone slot selected while you mine, it should get all your cobble. That could also obviate the need for the select being used in the above function, which would allow you to simplify it considerable and have it be faster.
thesonofdarwin #5
Posted 01 January 2013 - 07:43 AM
Aha, I did not know that! I could never understand how it was selecting slots. Thank you!!