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

Turtle Doing Weird Things.

Started by applesauce10189, 07 January 2014 - 05:07 PM
applesauce10189 #1
Posted 07 January 2014 - 06:07 PM
I have a mining turtle program. It will dig a staircase and optionally place torches if you want it to. I made it in a FTB modpack. I use the Ender Storage mod to keep it's inventory empty and to refuel it. The way it refuels and empties its inventory is it goes backwards then up and places the 2 chests and does as it should. Quick note it places the chests up/down hence going backwards then up. Now here's the problem, sometimes it only goes back once or not at all and when it doesn't go back as it should it skips the going up part entirely. One cause of this is a player standing in the way blocking it. But other than that it's entirely random and when it starts doing it, it doesn't stop, I've labeled the turtle and been in creative so I may test this and it is 100% random with no cause at all. It just happens and doesn't stop once it does but some turtles don't have this problem and they're code is 100% the same letter for letter, number for number, here's my code.

(Note. This program means a lot to me. It's my first program with a big goal. First to go above 20 lines of code. First to go above 100 lines of code. I want to continue this program until it is perfect in every way shape and form so I will be grateful for any and all help I can get.)


term.clear()
print("Place torches?")
print("1 = Yes 2 = No")
local x = read()
local test = 0
term.setCursorPos(1,3)
  if tonumber(x) == 1 then
    test = test + 1
  end
function placeTorches()
  if test == 1 then
    turtle.select(3)
    turtle.placeDown()
    turtle.select(1)
  end
end

function emptyInventory()
  for i = 4, 16 do
    turtle.select(i)
    turtle.dropDown(64)
  end
end

function gravityCheck()
  while turtle.dig do
    if turtle.dig() == false then
      break
    end
  end
end

function appleMine()
  while true do
    for i = 1,60 do
      turtle.select(1)
      placeTorches()
      turtle.digDown()
      turtle.down()
      turtle.digDown()
      turtle.down()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.forward()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.forward()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.forward()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.forward()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.back()
      turtle.back()
      turtle.up()
      turtle.placeDown()
      emptyInventory()
      appleFuel()
    end
  end
end
function appleFuel()
  if turtle.getFuelLevel() <= 50 then
    turtle.select(2)
    turtle.placeUp()
    turtle.suckUp()
    turtle.refuel(64)
    turtle.digUp()
  end
end
turtle.select(1)
appleMine()
Bomb Bloke #2
Posted 07 January 2014 - 08:13 PM
The logic appears to fall apart near the bottom of your loop:

      .
      .
      .
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.forward()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.back()
      turtle.back()
      turtle.up()
      turtle.placeDown()
      emptyInventory()
      appleFuel()
    end

I believe that attempt to move forward shouldn't be there. You likely won't see it actually go forward very often because most of the time there'll be a block in its way, but in the event that the space is clear the turtle will almost certainly get itself stuck.

On the first iteration of your "i" loop, the turtle will be unlikely to go up at the end there unless you manually clear out the block that'll likely be in its way. This will lead to the entire script messing up. If that space was manually cleared, then best I can make out the rest should be ok.

Some other (relatively minor) points, all this:

Spoiler
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.forward()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.forward()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.forward()
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.forward()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()

Could be re-written as something like:
Spoiler
    for j=1,4 do
      turtle.turnRight()
      turtle.dig()
      gravityCheck()
      turtle.turnLeft()
      turtle.turnLeft()
      turtle.dig()
      gravityCheck()
      turtle.turnRight()

      if j<4 then
        turtle.dig()
        gravityCheck()
        turtle.forward()
      end
    end

I like to make sure the turtle moves when I tell it to move, or at least pauses the script if it's unable to do what it needs to. You may consider making a few more functions near the top of your script:

Spoiler
local function forward()
  while not turtle.forward() do  -- So long as an attempt to go forward fails...
    turtle.dig()
    turtle.attack()
  end
end

local function back()
  if not turtle.back() then
    turtle.turnLeft()
    turtle.turnLeft()
    forward()
    turtle.turnLeft()
    turtle.turnLeft()
  end
end

… and also a couple of functions similar to the "forward()" one for going up/down. You then call these from then on instead of using turtle.forward() etc, and they'll either work, or the turtle will entirely stop instead of getting lost and confused.

Your torch-placing function should be called in such a way that it's not putting the things where the turtle is about to dig, but I assume you're aware of that and not worried about it at the moment.
applesauce10189 #3
Posted 08 January 2014 - 12:47 PM
The logic appears to fall apart near the bottom of your loop:

	  .
	  .
	  .
	  turtle.turnLeft()
	  turtle.turnLeft()
	  turtle.forward()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnRight()
	  turtle.back()
	  turtle.back()
	  turtle.up()
	  turtle.placeDown()
	  emptyInventory()
	  appleFuel()
	end

I believe that attempt to move forward shouldn't be there. You likely won't see it actually go forward very often because most of the time there'll be a block in its way, but in the event that the space is clear the turtle will almost certainly get itself stuck.

On the first iteration of your "i" loop, the turtle will be unlikely to go up at the end there unless you manually clear out the block that'll likely be in its way. This will lead to the entire script messing up. If that space was manually cleared, then best I can make out the rest should be ok.

Some other (relatively minor) points, all this:

Spoiler
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnLeft()
	  turtle.turnLeft()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.forward()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnLeft()
	  turtle.turnLeft()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.forward()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnLeft()
	  turtle.turnLeft()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.forward()
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnLeft()
	  turtle.turnLeft()
	  turtle.forward()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnRight()

Could be re-written as something like:
Spoiler
	for j=1,4 do
	  turtle.turnRight()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnLeft()
	  turtle.turnLeft()
	  turtle.dig()
	  gravityCheck()
	  turtle.turnRight()

	  if j<4 then
		turtle.dig()
		gravityCheck()
		turtle.forward()
	  end
	end

I like to make sure the turtle moves when I tell it to move, or at least pauses the script if it's unable to do what it needs to. You may consider making a few more functions near the top of your script:

Spoiler
local function forward()
  while not turtle.forward() do  -- So long as an attempt to go forward fails...
	turtle.dig()
	turtle.attack()
  end
end

local function back()
  if not turtle.back() then
	turtle.turnLeft()
	turtle.turnLeft()
	forward()
	turtle.turnLeft()
	turtle.turnLeft()
  end
end

… and also a couple of functions similar to the "forward()" one for going up/down. You then call these from then on instead of using turtle.forward() etc, and they'll either work, or the turtle will entirely stop instead of getting lost and confused.

Your torch-placing function should be called in such a way that it's not putting the things where the turtle is about to dig, but I assume you're aware of that and not worried about it at the moment.
Wow, lots of problems, thank you for pointing them out, typing turtle.(movement here)() was really repetitive so I must've typed forward by accident. I fixed the placement of the placeTorches function,