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

How to make my turtle stop moving when waiting for building blocks?

Started by ShoeLace1291, 20 February 2020 - 05:28 PM
ShoeLace1291 #1
Posted 20 February 2020 - 06:28 PM
So I am trying to make a script that builds four walls with whatever building blocks are provided. The problem I have with the script right now is that when the turtle's inventory runs out of building blocks, the turtle keeps moving forward along the path. How can I make the turtle stop moving when it runs out of building blocks?

This is my code:

write("How wide should the room be?")
local x = tonumber(read())
write("How long should the room be?")
local y = tonumber(read())
write("How tall should the room be?")
local z = tonumber(read())

write("Provide fuel and whatever building block you want to use and press enter to continue")

read()

local totalBlocks = ((x*2) + (y*2))*z
local minedBlocks = 0

function go()
    if not refuel() then
        return
    end
    if not forward() then
        return
    end
    if not place() then
        return
    end
    minedBlocks = minedBlocks +1
    percentComplete = minedBlocks / totalBlocks * 100
    if(percentComplete % 5 == 0) then
        print(percentComplete.."% Complete")
    end
end

function forward()
    if not turtle.forward() then
        write("Something is in the way!")
        return false
    end
    return true
end

function down()
    if not turlte.down() then
        write("Something is in the way!")
        return false
    end
    return true
end

function place()
    slot = 0
    while turtle.placeUp() == false do
        slot = slot + 1
        if slot == 17 then
            print("Waiting for more building blocks...")
            return false
        end
        turtle.select(slot)
    end
    return true
end

function refuel()
  while turtle.getFuelLevel() == 0 do
    slot = 0
    while(turtle.refuel(1) == false) do
      slot = slot + 1
      if slot == 17 then
        print("Waiting for more fuel...")
        return false
      end
      turtle.select(slot)
    end
  end
  return true
end

write("0% Complete...")

turtle.down()
for h=0,z,1 do
  for i=0,1,1 do
    for w=0,y,1 do
        go()
    end
    turtle.turnRight()
    for l=0,x,1 do
        go()
    end
    turtle.turnRight()
  end
  turtle.down()
end

print("Construction complete.")
qwerty #2
Posted 21 February 2020 - 12:18 PM
add something along the lines of:

for i=1,16 do
  turtle.select(i)
  local itemCount = itemCount + turtle.getItemCount(i)
end

if itemCount == 0 then pause
  else continue
end
Edited on 21 February 2020 - 11:18 AM
Lupus590 #3
Posted 23 February 2020 - 02:40 PM
lua doesn't have a continue statement

I would change your place method do that if there are no blocks then it goes into a while true loop which only breaks when there is a turtle_inventory event
qwerty #4
Posted 24 February 2020 - 09:24 AM
lua doesn't have a continue statement

I know, the "continue" there is just a representation, pseudo code if you will and I speciffically added "along the lines of", implying that that's only a template