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

My own mining program

Started by Aitkey, 01 January 2016 - 07:04 AM
Aitkey #1
Posted 01 January 2016 - 08:04 AM
I have gotten back into Feed The Beast Ultimate again and wrote a program to mine a 16x16 hole. I sorted out all the errors that were thrown and finally started the program, nothing happened. I was hoping that someone could help me with it.

My goal for this code was for the turtle to go forward 16 blocks while mining 3 blocks every time it moves forward, when it reaches the end of the 16 blocks it is supposed to turn, mine 3 blocks, move in the 3 blocks and then turn around and go back the way it came along the tunnel it just created.
The turtle would run until it ran out of chests to store the items in, which it would place every time the inventory filled to the max. It would do this by doing a 180 and placing the chest, and depositing all but the stored coal and chests in positions 1 and 2 respectively. The turtle would then turn back around the rest of the 360 and continue on mining. When the turtle finished it's 16x16x3 it would turn left to continue the pattern it had followed on the layer above and begin to go down 3 blocks to start a new layer of mining.

For now I have it refueling every time one of the functions is executed, I hope to make this a bit more efficient in the future.

I am also not sure if the turtle.suck method is being used properly and if it is actually needed at all, if someone could tell me whether or not the turtle automatically picks up the item, that would be appreciated.

I would appreciate any help and/ or tips concerning my code considering I have just started coding lua!

Here is the code:


function placeChest()
local firstItem = 3
local lastItem = 16

turtle.refuel()
turtle.turnLeft()
turtle.turnLeft()

if turtle.detect then
	turtle.dig()
end

turtle.place(2)

for i = firstItem,lastItem do
	turtle.select(i)
	turtle.drop(64)
end

turtle.turnLeft()
turtle.turnLeft()
end

function digSixteenBlocks()
local fullInv

for x = 1,16,1 do
	turtle.refuel()

	if turtle.detect then
	  turtle.dig()
	  turtle.suck()
	  fullInv = turtle.suck()

	  if fullInv == false then
		placeChest()
		turtle.suck()
	  end

	  turtle.forward()
	else
	  turtle.forward()
	end

	if turtle.detectUp then
	  turtle.digUp()
	  turtle.suckUp()
	  fullInv = turtle.suckUp()

	  if fullInv == false then
		placeChest()
		turtle.suckUp()
	  end

	end

	if turtle.detectDown then
	  turtle.digDown()
	  turtle.suckDown()
	  fullInv = turtle.suckDown()

	  if fullInv == false then
		placeChest()
		turtle.suckDown()
	  end

	end

end

end

function turnAround()
local fullInv

turtle.refuel()
turtle.turnLeft()

if turtle.detect then
	turtle.dig()
	turtle.suck()
	fullInv = turtle.suck()

	if fullInv == false then
	  placeChest()
	end

	turtle.forward()
else
	turtle.forward()
end

if turtle.detectUp then
	turtle.digUp()
	turtle.suckUp()
	fullInv = turtle.suckUp()

	if fullInv == false then
	  placeChest()
	end

end

if turtle.detectDown then
	turtle.digDonw()
	turtle.suckDown()
	fullInv = turtle.suckDown()

	if fullInv == false then
	  placeChest()
	end

end

turtle.turnLeft()
end

function goDown()
local fullInv

turtle.refuel()

for x = 0,2,1 do

	if turtle.detectDown then
	  turtle.digDown()
	  turtle.suckDown()
	  fullInv = turtle.suckDown()

	  if fullInv == false then
		placeChest()
	  end

	  turtle.down()
	end

end

turtle.turnLeft()
end



chestCount = turtle.getItemCount(2)

while not chestCount == 1 do

for i = 1,16,1 do
	digSixteenBlocks()
	turnAround()
	turtle.turnLeft()
end

goDown()
end
Bomb Bloke #2
Posted 01 January 2016 - 09:52 AM
while not chestCount == 1 do

"not" inverts the value after it; in this case, that value is "chestCount" (as opposed to "chestCount == 1"). Inverting a number-type gets you "false", and since false is never equal to one, your loop never executes its contents.

You meant:

while chestCount ~= 1 do
Aitkey #3
Posted 01 January 2016 - 11:06 PM
Thanks Bomb Bloke!

Okay so I have made some changes to the code and I am getting an error on line 158 that says "Expected Number" Can someone explain this?
Code:
Spoiler

function refuel()
  local fuelLevel
 
  fuelLevel = turtle.getFuelLevel()
 
  turtle.select(1)
 
  if fuelLevel < 5000 then
    turtle.refuel(5)
  end
 
end

function placeChest()
  local firstItem = 3
  local lastItem = 16

  refuel()
  turtle.turnLeft()
  turtle.turnLeft()

  if turtle.detect then
    turtle.dig()
  end

  turtle.select(2)
  turtle.place(2)

  for i = firstItem,lastItem do
    turtle.select(i)
    turtle.drop(64)
  end

  turtle.turnLeft()
  turtle.turnLeft()
end

function digForward()
  local fullInv

  for x = 1,16,1 do
    refuel()

    if turtle.detect then
	  turtle.dig()
	  turtle.suck()
	  fullInv = turtle.suck()

	  if fullInv == false then
	    placeChest()
	    turtle.suck()
	  end

	  turtle.forward()
    else
	  turtle.forward()
    end

    if turtle.detectUp then
	  turtle.digUp()
	  turtle.suckUp()
	  fullInv = turtle.suckUp()

	  if fullInv == false then
	    placeChest()
	    turtle.suckUp()
	  end

    end

    if turtle.detectDown then
	  turtle.digDown()
	  turtle.suckDown()
	  fullInv = turtle.suckDown()

	  if fullInv == false then
	    placeChest()
	    turtle.suckDown()
	  end

    end

  end

end

function turnAround()
  local fullInv

  refuel()
  turtle.turnLeft()

  if turtle.detect then
    turtle.dig()
    turtle.suck()
    fullInv = turtle.suck()

    if fullInv == false then
	  placeChest()
    end

    turtle.forward()
  else
    turtle.forward()
  end

  if turtle.detectUp then
    turtle.digUp()
    turtle.suckUp()
    fullInv = turtle.suckUp()

    if fullInv == false then
	  placeChest()
    end

  end

  if turtle.detectDown then
    turtle.digDown()
    turtle.suckDown()
    fullInv = turtle.suckDown()

    if fullInv == false then
	  placeChest()
    end

  end

  turtle.turnLeft()
end

function goDown()
  local fullInv

  refuel()
 
  for x = 0,2,1 do

    if turtle.detectDown then
	  turtle.digDown()
	  turtle.suckDown()
	  fullInv = turtle.suckDown()

	  if fullInv == false then
	    placeChest()
	  end

	  turtle.down()
    end

  end

  turtle.turnLeft()
end


turtle.select(2)
chestCount = turtle.getItemCount()

while  chestCount ~= 1 do

  for i = 1,16,1 do
    digForward()
    turnAround()
    turtle.turnLeft()
  end

  goDown()
end

And here is the line that is throwing the error

Spoiler

chestCount = turtle.getItemCount()
Bomb Bloke #4
Posted 02 January 2016 - 02:52 AM
I've got a vague memory that older versions of ComputerCraft may want you to specify a slot number, eg:

chestCount = turtle.getItemCount(2)

Failing that: Double check that the full error message is referring to your script, and not some API.
Aitkey #5
Posted 05 January 2016 - 05:04 AM
Ok so I have changed my code a bit and right now I am getting an error that says

"miningProgram:4: attempt to compare __lt on function and number"

Here is the code:


function refuel()
  turtle.select(1)
 
  if turtle.getFuelLevel < 5000 then
    turtle.refuel(5)
  end
 
end

function placeChest()
  local firstItem = 3
  local lastItem = 16

  refuel()
  turtle.turnLeft()
  turtle.turnLeft()

  if turtle.detect then
    turtle.dig()
  end

  turtle.select(2)
  turtle.place(2)

  for i = firstItem,lastItem do
    turtle.select(i)
    turtle.drop(64)
  end

  turtle.turnLeft()
  turtle.turnLeft()
end

function check16()
  turtle.select(16)
 
  if turtle.getItemCount > 1 then return true
  else return false
  end
 
end

function digForward()

  for x = 1,16,1 do
    refuel()

    if turtle.detect then
	  turtle.dig()
	  turtle.forward()
      
      if check16()
   	 placeChest()
      end
      
    else
	  turtle.forward()
    end

    if turtle.detectUp then
	  turtle.digUp()
      
      if check16()
   	 placeChest()
      end
      
    end

    if turtle.detectDown then
	  turtle.digDown()
      
      if check16()
   	 placeChest()
      end
      
    end

  end

end

function turnAround()
  refuel()
  turtle.turnLeft()

  if turtle.detect then
    turtle.dig()
    turtle.forward()
    
      if check16()
   	 placeChest()
      end
      
  else
    turtle.forward()
  end

  if turtle.detectUp then
    turtle.digUp()
    
      if check16()
   	 placeChest()
      end
      
  end

  if turtle.detectDown then
    turtle.digDown()
    
      if check16()
   	 placeChest()
      end
      
  end

  turtle.turnLeft()
end

function goDown()
  refuel()
 
  for x = 0,2,1 do

    if turtle.detectDown then
	  turtle.digDown()
	  turtle.down()
      
      if check16()
   	 placeChest()
      end
      
    end

  end

  turtle.turnLeft()
end


turtle.select(2)
chestCount = turtle.getItemCount()

while  chestCount ~= 1 do

  for i = 1,16,1 do
    digForward()
    turnAround()
    turtle.turnLeft()
  end

  goDown()
end
Dog #6
Posted 05 January 2016 - 05:34 AM
On line 4 you forgot to add the brackets after turtle.getFuelLevel - it should be turtle.getFuelLevel()

Basically you weren't calling the function (using the brackets) so you were comparing the function to a number (in this case, 5000) instead of comparing the output of the function to a number (which is what you want).