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

Turtle 3x3 Strip miner (HELP!)

Started by SexyBananaSX, 26 October 2015 - 10:50 PM
SexyBananaSX #1
Posted 26 October 2015 - 11:50 PM
Recently I developed a code to make a 3x3 hallway at whatever length you want with 3x3 "branches" going as deep as you want; For the most part it runs amazingly. However, when it's suppose to finish the last branch it starts making an additional one, THEN it tries to head to the beginning but can't make it.

Could someone please look over it and help me make it better? Thank you in advance.

I made my code unique in the sense that it needs 3 different enderchest (from EnderStorage mod) one for item dumping, one for fuel, and one for torches

Slot: 1 > enderchest for item dumping only
Slot: 2 > enderchest for fuel only (must have fuel inside)
Slot: 3 > enderchest for torches only (must have torches in it)
Slot: 4 > at least 1 torch (torches have to be in the torches chest in slot 3 for this to work)

Original code

local tArgs = { ... }
if #tArgs~= 2 then
  print("  Usage: stripper <Hallway length> <Tunnel Length>")
  return
end
local hallway = tonumber( tArgs[1] )
local mines = tonumber( tArgs[2] )
if hallway < 1 then
  print(" Tunnel length must be positive!")
  return
end
if mines < 1 then
  print(" Number of mines must be positive!")
  return
end
local torch = turtle.getItemCount(4)
local hallLength = 0
local minesLength = 0
local torchCounter = 0
--ItemDump must have a secondary enderchest from which items will be pulled from
function itemDump()  -- Places down designated Enderchest to get rid of ITEMS mined
  if turtle.getItemCount(16) > 0 then
	turtle.select(1)
	turtle.digDown()
	turtle.placeDown()
	for slot = 6,16,1 do
	  turtle.select(slot)
	  turtle.dropDown()
	end
	turtle.select(1)
	turtle.digDown()
	turtle.select(5)
	turtle.placeDown()
  end
end
function fuel()
  if turtle.getFuelLevel() < 10 then
	turtle.select(2)
	turtle.digDown()
	turtle.placeDown()
	turtle.select(2)
	for f=1,5,1 do
	  while not suckDown(1) do
		print("No FUEL discovered!")
		print("Waiting on FUEL!")
	  end
	  sleep(0.5)
	  turtle.refuel()
	  sleep(0.2)
	end
	print("Turtle refueled!")
	term.clear()
	turtle.digDown()
  end
end
function placeTorch()
  turtle.turnLeft()
  while not turtle.forward() do
	turtle.dig()
	sleep(0.4)
	itemDump()
  end
  fuel()
  turtle.turnLeft()
  turtle.select(4)
  turtle.place()
  turtle.turnLeft()
  while not turtle.forward() do
	turtle.dig()
	sleep(0.4)
	itemDump()
  end
  turtle.turnLeft()
  torch = turtle.getItemCount()
end
function cTorch()
  if torch < 5 then
	turtle.select(3)
	turtle.digDown()
	turtle.placeDown()
	turtle.select(4)
	for o=1,30,1 do
	  while not turtle.suckDown(1) do
		print("No TORCHES discovered!")
		print("Waiting for TORCHES!")
		sleep(5)
		term.clear()
	  end
	end
	print("Torches aquired!")
	term.clear()
	turtle.select(3)
	turtle.digDown()
	turtle.select(5)
	turtle.placeDown()
	cTorch()
  else
	placeTorch()
  end
end
function blockDetection()
  while turtle.detect() do
	turtle.dig()
	itemDump()
	sleep(0.5)
  end
end
function up()
  while not turtle.up() do
	turtle.digUp()
	itemDump()
	sleep(0.5)
  end
  fuel()
end
function down()
  while not turtle.down() do
	turtle.digDown()
	itemDump()
	sleep(0.5)
  end
  fuel()
end
function forward()
  while not turtle.forward() do
	turtle.dig()
	itemDump()
	sleep(0.5)
  end
  fuel()
end
function nextPhase()
  turtle.turnLeft()
  turtle.turnLeft()
  for i=1,hallLength do
	forward()
  end
  turtle.turnLeft()
  turtle.turnLeft()
end
function stripEnd()
  turtle.turnLeft()
  turtle.turnLeft()
  for i=1,minesLength() do
	forward()
  end
  turtle.turnLeft()
end
function destroyColumn()
  blockDetection()
  up()
  blockDetection()
  up()
  blockDetection()
  blockDetection()
  down()
  blockDetection()
  down()
  blockDetection()
end
function destroy()
  destroyColumn()
  turtle.turnLeft()
  forward()
  turtle.turnRight()
  destroyColumn()
  turtle.turnLeft()
  forward()
  turtle.turnRight()
  destroyColumn()
  turtle.turnRight()
  forward()
  forward()
  turtle.turnLeft()
  forward()
end
function home()
  turtle.turnLeft()
  turtle.turnLeft()
  for i=1,hallway,1  do
	forward()
  end
  turtle.turnLeft()
  turtle.turnLeft()
end
function nstrip()
  turtle.turnLeft()
  turtle.turnLeft()
  for i=1,mines do
	forward()
  end
  forward()
  forward()
  turtle.turnLeft()
end
function run()
  print("Do you have...")
  print("Enderchest in SLOT:1 for item dumping?")
  print("Enderchest in SLOT:2 for fuel aquiring?")
  print("Enderchest in SLOT:3 for torch getting?")
  textutils.slowPrint("Starting in 10 seconds!")
  textutils.slowPrint("Hold 'CTRL + T' to abort!")
  sleep(10)
  for i=1,hallway do
	destroy()
	torchCounter = torchCounter + 1
	if torchCounter = 10 then
	  cTorch()
	  torchCounter = 0
	end
  end
  home()
  while hallLength < hallway do
	for i=1,6 do
	  forward()
	  hallLength = hallLength + 1
	end
	turtle.turnLeft()
	forward()
	forward()
	for h=1,mines do
	  destroy()
	  torchCounter = torchCounter + 1
	  if torchCounter = 9 then
		cTorch()
		torchCounter = 0
	  end
	end
	nstrip()
  end
  home()
end
———————————————————————————————————————————————————————————————
Edited on 30 October 2015 - 07:45 PM
Lyqyd #2
Posted 27 October 2015 - 12:39 AM
Moved to Ask a Pro.
Bomb Bloke #3
Posted 28 October 2015 - 01:46 AM
Without anything calling run(), I'm surprised it does much at all.

cTorch() contains an infinite loop where it may keep calling itself - if torch < 5, then it repeats while never altering the value of torch. It will've collected at least thirty of the things on each iteration anyway, making the recursive loop redundant in the first place.

destroyColumn() seems to be filled with redundant calls. Why call blockDetection() twice in a row, when it already digs out everything to be dug? Why call it at all while going up, when doing so on the way down is sufficient?

However, when it's suppose to finish the last branch it starts making an additional one, THEN it tries to head to the beginning but can't make it.

If I'm reading things correctly - and I may well not be - nstrip() makes new branches? If so, check if you're on the last run and break out of your "while hallLength < hallway do" loop before hitting that last line. If not, break earlier.
SexyBananaSX #4
Posted 30 October 2015 - 08:18 PM
Thank you for your reply, Bomb Bloke.
When I was testing, I forgot to put update my file with run(). lol
Because of you, I now know my mistake with the cTorch() function, for which I thank you.
As for the blockDetection(), I was taking extra steps to ensure no gravel/sand would cause problems as the turtle operated.

nstrip() brings the turtle back to the start of the strip so it can resume down the hallway and make another strip. Does that make sense? probably not.
However, I do believe it may have something to do with the reason why it makes an extra strip then what it's suppose to… but I'm not sure how.

This code has a lot more bugs then I realized, I'm going to try and fix what I can then get back when I can.