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

Tunnel... (Default program.)

Started by Lost Ninja, 16 October 2012 - 08:26 AM
Lost Ninja #1
Posted 16 October 2012 - 10:26 AM
Newish user of CC here, done a bit of programming in the past some of it with lua so can read the structure of a program even if I don't quite get what is going on 100%. Been playing with the default (included) Tunnel program trying to get a couple more features into it. Specifically a RTB feature that in the original is commented out. I got it to work but am having issues with fueling. Can anyone give me some help?

Spoiler
-- A revised version of the built in tunnelling script that mines a 3x3 tunnel (as opposed to 3x2)
-- Fills in any missing floor blocks with blocks from slot 1 (in theory)
-- returns to start point (buggy won't refuel)
-- Mk III v0.1
-- changes by LN

local tArgs = { ... }
if #tArgs ~= 1 then  -- No length supplied error out with message
  print( "Usage: tunnel <length>" )
  return
end

-- If value supplied check to make sure it's positive
local length = tonumber( tArgs[1] )
if length < 1 then
  print( "Tunnel length must be positive" )
  return
end

-- some more variables (wtf is depth?)
local depth = 0
local collected = 0
local dbg = 0 -- change to 1 if you want test messages to be displayed (LN)

local function collect() -- function works out how much shit you've collected (fails with lapis &amp; redstone iirc)
  collected = collected + 1
  if math.fmod(collected, 25) == 0 then
	print( "Mined "..collected.." items." )
  end
end

local function test(txt) -- function to print test/debug messages, bit spammy but shows what turtle thinks it's doing... even when it isn't... :D/>/> (LN)
  if dbg == 1 then
	print("Turtle says: " , txt )
	sleep(5) -- change to shorter/longer to speed up/slow down debug
  end
end

local function reverseTurtle() -- 180 degree turn (LN)
  turtle.turnRight()
  turtle.turnRight()
  test("Reversing Turtle")
end

local function tryDig()
  while turtle.detect() do
	if turtle.dig() then
	  collect()
	  sleep(0.5)
	  test("Dig Fwd")
	else
	  test("Dig Fwd Fail")
	  return false
	end
  end
  return true

end

local function tryDigUp()
  while turtle.detectUp() do
	if turtle.digUp() then
	  collect()
	  sleep(0.5)
	  test("Dig Up")
	else
	  test("Dig Up Fail")
	  return false
	end
  end
  return true

end

local function tryDigDown()
  while turtle.detectDown() do
	if turtle.digDown() then
	  collect()
	  sleep(0.5)
	  test("Dig Down")
	else
	  test("Dig Down Fail")
	  return false
	end
  end
  return true

end

local function refuel()
  local fuelLevel = turtle.getFuelLevel()
  if fuelLevel == "unlimited" or fuelLevel > 0 then
	return
  end

  local function tryRefuel()
	for n=1,16 do
	  if turtle.getItemCount(n) > 0 then
		turtle.select(n)
		if turtle.refuel(1) then
		  turtle.select(1)
		  return true
		end
	  end
	end
	turtle.select(1)
	return false
  end

  if not tryRefuel() then
	print( "Add more fuel to continue." )
	while not tryRefuel() do
	  sleep(1)
	end
	print( "Resuming Tunnel." )
  end
end

local function tryUp()
  refuel()
  while not turtle.up() do
	if turtle.detectUp() then
	  if not tryDigUp() then
		test("Move Up &amp; Dig")
		return false
	  end
	elseif turtle.attackUp() then
	  collect()
	  test("Attacking Up")
	else
	  sleep( 0.5 )
	end
  end
  return true

end

local function tryDown()
  refuel()
  while not turtle.down() do
	if turtle.detectDown() then
	  if not tryDigDown() then
		test("Move Down &amp; Dig")
		return false
	  end
	elseif turtle.attackDown() then
	  collect()
	  test("Attacking Down")
	else
	  sleep( 0.5 )
	end
  end
  return true

end

local function tryForward()
  refuel()
  while not turtle.forward() do
	if turtle.detect() then
	  if not tryDig() then
		test("Move Fwd &amp; Dig")
		return false
	  end
	elseif turtle.attack() then
	  collect()
	  test("Attacking Fwd")
	else
	  sleep( 0.5 )
	end
  end
  return true

end

print( "Tunnelling..." )

--[[ Tunnel cross section
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
Turtle starts at 7, digs 4, moves 4, digs 1, moves 1, digs 2, moves 2, digs 5 &amp; 3, moves 3, digs 6, moves 6, 9 -> 9, 8 -> 8... and back to 7. Places blocks below at 7, 9 , 8. (in that order)
]]

for n=1,length do
  turtle.placeDown()
  test("Place A")
  tryDigUp()
  tryUp()
  tryDigUp()
  tryUp()
  turtle.turnRight()
  test("Turning Right")
  tryDig()
  tryForward()
  tryDigDown()
  tryDig()
  tryForward()
  tryDigDown()
  tryDown()
  tryDigDown()
  tryDown()
  reverseTurtle()
  test("Reversing direction faced")
  tryDig()
  turtle.placeDown()
  test("Place Down C")
  tryForward()
  turtle.placeDown()
  test("Place Down B")
  tryForward()
  turtle.turnRight()
  test("Turning Right")

  if n<length then
	tryDig()
	if not tryForward() then
	  print( "Aborting Tunnel." )
	  break
	end
  else
	print( "Tunnel complete." )
	print( "Returning to start..." )

-- Return to where we started
-- This section is not 100% correct. It should fuel when low on fuel and doesn't, though the tryForward works everywhere else.
-- Does work if the fuel doesn't run out, could just add a manual refuel but that would mean a)in long distances it'd still run out, b)in short dsiatnces I waste fuel... :P/>/>

	reverseTurtle()
	  while length > 0 do
		if tryForward() then
		  length = length - 1
		else
		tryDig()
		end
	  end
	reverseTurtle()
  end
end

print( "Tunnel complete." )
print( "Mined "..collected.." items total." )
((Apologies for all the eroneous comments they're mostly to make it clear to me what I'm doing…))
http://pastebin.com/xJ7VMe40

I thought by using the tryForward function that would handle the fuelling as it does in the rest script but it still runs out of fuel. Next I figured I could just call the refuel function (which works) but if the turtle is either a long way off (100+) it still stops short of the full RTB, while if it's close it wastes the fuel when it is inevitably broken to reposition.

While any help is of course appreciated I'd really appreciate if you don't suggest adding an additional API because I'd much rather get it working with just the code you start out with.
Ditto8353 #2
Posted 16 October 2012 - 03:07 PM
I suggest you start writing the program from scratch. You will be able to easily understand what all of the code does and you can improve on algorithms where needed. The original code could work as a handy reference, but adding new features to code that isn't yours is always a pain.

Note: A more efficient digging algorithm starts bottom center and turns to the side. It digs out the side while it digs up, then turns around and moves down while it digs out the other side.
Lost Ninja #3
Posted 16 October 2012 - 03:14 PM
I suggest you start writing the program from scratch. You will be able to easily understand what all of the code does and you can improve on algorithms where needed. The original code could work as a handy reference, but adding new features to code that isn't yours is always a pain.
I'll give it a shot but don't think I understand the turtle well enough to mange it.

Note: A more efficient digging algorithm starts bottom center and turns to the side. It digs out the side while it digs up, then turns around and moves down while it digs out the other side.
Which is the way I had it to start with - crap coder I may be, stupid… I am too… :D/>/> - However I want the floor added in too, eventually I'll have it add walls/ceiling. Seems that the only block it doesn't need to travel through is the centre block, hence the way I have it set up now. I would prefer a more adaptable method for working this out so I could handle bigger tunnels… but that will come as I gain more confidence… maybe. :P/>/>

TY for the help anyway.
Ditto8353 #4
Posted 16 October 2012 - 03:40 PM
I'll give it a shot but don't think I understand the turtle well enough to mange it.

TY for the help anyway.

Just play around with it for a bit. Learn the use of for loops like the back of your hand and you should have all the tools you need. The hardest part of turtles I have run into is dealing with variable dimensions for tunneling and tracking relative positions without GPS so the turtle knows how to get home.

Good luck!
Lost Ninja #5
Posted 16 October 2012 - 10:14 PM
Digging a straight tunnel as long as each full clear (that is the cross section) has the turtle end in the same spot (or a spot you know beforehand) getting back to the start is just a matter of recording how far 'in' it's dug. That's the easy bit. Knowing the the rest is where I get stuck… :D/>/>

Thanks for the help…

I'm going inside… I will be some time… :P/>/>
slymon99 #6
Posted 10 December 2012 - 02:01 PM
How do you edit the default programs? I tried cp rom/programs/excavate myExcavate but it didn't work.