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

[QUESTION] Turtle glitching mid-program.

Started by Lightor, 27 February 2013 - 04:33 PM
Lightor #1
Posted 27 February 2013 - 05:33 PM
Title: [QUESTION] Turtle glitching mid-program.

I wrote my first bit of LUA for CC, I have some experience with C++ so it wasn't too rough but I seem to be having a weird run-time error that I can't seem to nail down, so hopefully someone here can help me. So the program digs what I believe is an efficient mining tunnel. It digs a main hallway of 3 high by 2 wide, with branches going out every 3rd row. Now it digs the first bit of hallway and first 2 branches fine but when it starts the 3rd branch it freaks. It spins around then digs up for some reason, I'm not sure why because its basically just looping what it's already done. it looks like it digs up once, then instead of digging down, it turns around twice, thinking its dug both branches, then starts the next hall.

Below is my code, thanks for the help in advance guys.


-- Variables
turtle.select(1)

local tArgs = { ... }
distance = tonumber(tArgs[1])
branches = tonumber(tArgs[2])
distance = distance or 1
branches = branches or 5
local traveled = 0;

print("Work, work!")

-- Functions --

--Fuels the turtle
function tFuel(amount)

if turtle.getItemCount(1) > 1 then
  if turtle.getFuelLevel() < 3 then
   turtle.refuel(amount)
   end
else
   print "Out of fuel!"
  return
end

end

--Turn around
function turnAround()
   turtle.turnRight()
   turtle.turnRight()
end

--Checks if it needs to dig, else it just moves
function tryDig(dir)

   -- Forward
if dir == "F" then
  if turtle.detect() then
   turtle.dig()
   sleep(0.25)
   turtle.forward()
  else
   turtle.forward()
  end

	-- Up
else if dir == "U" then
  if turtle.detectUp() then
   turtle.digUp()
   sleep(0.25)
   turtle.up()
  else
   turtle.up()
  end

	-- Down
else if dir == "D" then
  if turtle.detectDown() then
   turtle.digDown()
   sleep(0.25)
   turtle.down()
  else
   turtle.down()
  end
end
end
end
end

--Digs the hall between branches
function digBranch()

tryDig("F")

tryDig("U")
tryDig("U")

turtle.turnRight()

i = 0
dir = "D"

--Dig branch
while i < branches do
  tryDig("F")
  tryDig(dir)
  tryDig(dir)

  --Swap direction
  if dir == "D" then
   dir = "U"
  else
   dir = "D"
  end

  i = i + 1
  sleep(0.25)
end

--Bring turtle to bottom
if dir == "D" then
  turtle.down()
  turtle.down()
  dir = "U"
end

turnAround()

--Go back to main hall
i = 0
while i < branches do
  tryDig("F")

  i = i + 1
  sleep(0.25)
end

i = 0

--Dig rest of hall and branch
while i < (branches + 1) do
  tryDig("F")
  tryDig(dir)
  tryDig(dir)

  --Swap direction
  if dir == "D" then
   dir = "U"
  else
   dir = "D"
  end

  i = i + 1
end

--Bring turtle to bottom
if dir == "D" then
  turtle.down()
  turtle.down()
  dir = "U"
end

turnAround()

--Go back to main tunnel
i = 0
while i < branches do
  turtle.forward()
  i = i + 1
end

i = 0

--Prep and Turn to prep for hall dig
turtle.forward()
turtle.turnLeft()
end

--Digs the hall between branches
function digHall()

--Up, one forward
tryDig("F")
turtle.turnLeft()
tryDig("F")
turtle.turnRight()

tryDig("U")
turtle.turnRight()
tryDig("F")
turtle.turnLeft()

tryDig("U")
turtle.turnLeft()
tryDig("F")
turtle.turnRight()

--Down, one forward
tryDig("F")
turtle.turnRight()
tryDig("F")
turtle.turnLeft()

tryDig("D")
turtle.turnLeft()
tryDig("F")
turtle.turnRight()

tryDig("D")
turtle.turnRight()
tryDig("F")
turtle.turnLeft()

end

-- Starting Out --
if turtle.getItemCount(1) >= 1 then
for x = 0, distance do
  tFuel(1)

  digHall()
  traveled = traveled + 2
  x = x + 2

  digBranch()
  traveled = traveled + 1
  x = x + 1
end
else
print "No fuel = no dig."
end
LordIkol #2
Posted 28 February 2013 - 05:28 AM
Hi Lightor,

At the moment im at work so i can not test the Code. And for me its a little bit to confusing to read your Code while Work.
I will check it when im home and see if i can help you with this.

If i understand you right than you want to start the program like this "branchmining 30 10" and then it starts digging out a tunnel 30 Long 2 wide 3 high and every 3rd row dig a branch same size is this correct?

if this is what you want to do i would try sth like this (not tested and written at work but i think you will get the point)

sorry code tags did not work ;)/>

-- Variables
turtle.select(1)

local tArgs = { ... }
distance = tonumber(tArgs[1])
distance = distance or 1
print("Work, work!")

-- Functions --

--Fuels the turtle
function tFuel(amount)
if turtle.getItemCount(1) &amp;gt; 1 then
  if turtle.getFuelLevel() &amp;lt; 3 then
   turtle.refuel(amount)
   end
else
   print "Out of fuel!"
  return
end
end

-- move forward make sure there is no gravel stopping the turtle fuelcheck implemented here
local function moveForward()
	while turtle.detect() do
	turtle.dig()
	end
	moved = false
	while not(moved) do
		moved = turtle.forward()
	end
	tFuel(1)
end

local function digginUpnDown()
while turtle.detectUp() do
turtle.digUp()
end
turtle.digDown()
end

local function digBranch()
for b = 0,distance do
moveForward()
digginUpnDown()
end
turtle.turnLeft()
moveForward()
turtle.turnLeft()
for b = 0,distance do
moveForward()
digginUpnDown()
end
end

local function digHall()
for h = 0,distance do
moveForward()
digginUpnDown()
if h%3 == 0 then
turtle.turnRight()
digBranch()
h = i+1
turtle.turnRight()
end
end
turtle.turnLeft()
moveForward()
turtle.turnLeft()
for h = 0,distance do
moveForward()
digginUpnDown()
if h%3 == 0 then
turtle.turnRight()
digBranch()
h = i+1
turtle.turnRight()
end
end
end

digHall()
Lightor #3
Posted 28 February 2013 - 06:17 AM
Hey Lordlkool,

The program would indeed start like branchmind 10 5.

This would cause it to mine a tunnel that is 2 wide, and 3 tall, going 10 blocks deep into the mountainside. Then using the 5, it would make a branch every 3 blocks that is 5 deep, 3 high and 1 wide.
I feel like a picture might better help, so here is a small section of what I'm going for.












P.S. - Great point you made about the gravel and sand falling, I'll work that check in.
LordIkol #4
Posted 28 February 2013 - 11:51 AM
Hi Lightor,

nearly forgot you :D/>/>
i tested my code and fixed some errors and put in your need for the lenght of the branches.
then i added a 3rd argument for looping. means when you type like branch 8 3 2 (stardart setup when you not put in arguments)
he will dig down 5 then a tunnel 8 long and after every 4th step he will dig a branch that is 3 long 2 wide 3 high.
the reason why use 2 wide branch is that you have to move back the way so or so why not use the way to show one more row. pattern keeps the same 2 walls between each branch.
and he will do this 2 times so 10 deep.

When he is finished he will go back up 1 above the starting point.

Im not a pro but i hope it does what you need :)/>/>

EDIT: added option to place ladders so you can get down to the branches more easy and fixed the fuelcheck script put 1 in as 4th argument to use ladder else leave blank for no ladders.

http://pastebin.com/jhNU63G0

-- Variables
local tArgs = { ... }
distance = tonumber(tArgs[1])
distance = distance or 8
branchl = tonumber(tArgs[2])
branchl = branchl or 3
loops = tonumber(tArgs[3])
loops = loops or 2
ladder = tonumber(tArgs[4])
ladder = ladder or 0

-- checks if ladder is in slot 2 if param is set
function checkmaterials()
if ladder == 1 then
while turtle.getItemCount(2) <= 5*ladder do
print("Please put " .. 5*loops.. " ladder in slot 2")
sleep(5)
end
end
end
-- Functions --

--Fuels the turtle and waits for fuel if nothing left
function tFuel(amount)
if turtle.getItemCount(1) > 1 then
  if turtle.getFuelLevel() < 3 then
  turtle.select(1)
   turtle.refuel(amount)
   end
else
while turtle.getItemCount(1) < 1 do
   print "Out of fuel!"
   print "put fuel in slot 1"
   sleep(5)
end
end
end

-- move forward make sure there is no gravel stopping the turtle fuelcheck implemented here
local function moveForward()
		while turtle.detect() do
		turtle.dig()
		end
		moved = false
		while not(moved) do
				moved = turtle.forward()
		end
		tFuel(1)
end

-- same like forward
function moveUp()
turtle.digUp()
moved = false
while not(moved) do
while turtle.detectUp() do
  turtle.digUp()
end
moved = turtle.up()
end
tFuel(1)
end

-- in case there is a mob or player better include notmoved
local function moveDown()
		for i = 1,5 do
		turtle.digDown()
		moved = false
		while not(moved) do
				moved = turtle.down()
		end
		end
tFuel(1)
end

-- just cause im lazy for typing :D/>/>/>/>
local function digginUpnDown()
while turtle.detectUp() do
turtle.digUp()
end
turtle.digDown()
end

-- digs out the branch on both sides
local function digBranch()
for b = 1,branchl do
moveForward()
digginUpnDown()
end
turtle.turnLeft()
moveForward()
turtle.turnLeft()
digginUpnDown()
for b = 1,branchl do
moveForward()
digginUpnDown()
end
moveForward()
digginUpnDown()
for b = 1,branchl do
moveForward()
digginUpnDown()
end
turtle.turnLeft()
moveForward()
turtle.turnLeft()
digginUpnDown()
for b = 1,branchl do
moveForward()
digginUpnDown()
end
moveForward()
digginUpnDown()
end

-- dig the main Hall
local function digHall()
for h=0, distance do
moveForward()
digginUpnDown()
if h%4 == 0 then
turtle.turnRight()
digBranch()
turtle.turnLeft()
end
end
turtle.turnLeft()
moveForward()
turtle.turnLeft()
digginUpnDown()
for h=0,distance do
moveForward()
digginUpnDown()
end
turtle.turnLeft()
moveForward()
turtle.turnLeft()
digginUpnDown()
end

-- going up to starting point again
local function goback()
if ladder == 1 then
turtle.turnLeft()
turtle.turnLeft()
end

for i =1, loops*5+1 do
if ladder == 1 then
turtle.select(2)
turtle.dig()
turtle.place()
moveUp()
else
moveUp()
end
end
end

-- code excecution
print("Work, work!")
checkmaterials()
for i=1,loops do
tFuel(1)
moveDown()
digHall()
end
goback()
Lightor #5
Posted 28 February 2013 - 11:05 PM
Thanks for all the work you put into this! I see you put a few extra things in there, I'll have to test it and see how it goes.

Any idea what caused my original code not to work? I'm assuming you ran it and saw the issue it was having.
LordIkol #6
Posted 28 February 2013 - 11:51 PM
No Problem i like to code and a branch mining script is always good to have :)/>
Im sorry but i did not test your Code yesterday started with the Code snipped i posted when i was at work.

But when im back home today i will check up your Code and see if i can find the error :)/> (i hate it too when i dont know why stuff is not working :D/>)
for now i can not see why its glitching looks correct to me
Lightor #7
Posted 01 March 2013 - 12:21 AM
Ok thanks, yah I just want to understand the runtime error I'm getting to better avoid it in the future.