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

Buggy Mining/Printing code.

Started by Ikimiko, 21 March 2013 - 02:20 PM
Ikimiko #1
Posted 21 March 2013 - 03:20 PM
Title: Buggy Mining/Printing code.

So, I’m completely new to code and have been working on a code to mine the ‘length and width’ chosen; however, it seems to be full of bugs. I’ve come to the point where I’m just scratching my head wondering why it’s not working. If I could get some help on this and any pointers you feel like sharing that would be great.

The bugs that I know about are:
o Printing amount of blocks stays at “Mined 1 block” when I was trying to get it to count and print the total blocks mined to that point.
o When mining a straight line turtle continues forever. (I believe this is because finish and length aren’t being compared correctly)
o When mining a length of 3 and width of 3 the turtle goes in a square like this:
>>>>
^__ v ← Start, forward being down.
<<<<
It's supposed to look like this:
__v ← Start, forward being down.
v<<
>>v
v<< ← And then come back to the starting position.

Anyway, here’s the code:



--Declare locals.
local tArgs = {...}
local length = tArgs[1]
local width = tArgs[2]
   width = width - 1
local face = 1
local blocks = 0
local finish = 0


--Create functions.
function left(face)
   if turtle then turtle.turnLeft() end
	  face = face - 1
   if face < 1 then face = face + 4 end
   return face
end

function right(face)
   if turtle then turtle.turnRight() end
   face = face + 1
   if face > 4 then face = face - 4 end
   return face
end

function addB(blocks)
   blocks = blocks + 1
   if blocks > 1 then
	  print (‘Mined ‘..blocks..’ blocks.’)
   end
   if blocks == 1 then
	  print (‘Mined ‘..blocks..’ block.’)
   end
   return blocks
end

function digWidth(width)
   For a = 1, width do
	  while turtle.detect() == true do
		  turtle.dig()
		  addB(blocks)
		  sleep(0.5)
	  end
	  turtle.forward()
	  while turtle.detectUp() == true do
		  turtle.digUp()
		  addB(blocks)
		  sleep(0.5)
	  end
	  while turtle.detectDown() == true do
		  turtle.digDown()
		  addB(blocks)
		  sleep(0.5)
	  end
   end
end

function walkWidth(width)
   For dNCT = 1, width do
	  turtle.forward()
   end
end

--Execute code.
while finish ~= length do
   if face == 1 then
	  while turtle.detect() == true do
		 turtle.dig()
		 addB(blocks)
		 sleep(0.5)
	  end
	  turtle.forward()
	  while turtle.detectUp() == true do
		 turtle.digUp()
		 addB(blocks)
		 sleep(0.5)
	  end
	  while turtle.detectDown() == true do
		 turtle.digDown()
		 addB(blocks)
		 sleep(0.5)
	  end
	  finish = finish + 1
	  if width > 0 then
		 right(face)
		 digWidth(width)
		 if finish == length then
			left(face)
			left(face)
			walkWidth(width)
			right(face)
		 end
	  end
   end
   if finish ~= length then
	  if face == 2 then
		 left(face)
		 while turtle.detect() == true do
			turtle.dig()
			addB(blocks)
			sleep(0.5)
		 end
		 turtle.forward()
		 while turtle.detectUp() == true do
			turtle.digUp()
			addB(blocks)
			sleep(0.5)
		 end
		 while turtle.detectDown() == true do
			turtle.digDown()
			addB(blocks)
			sleep(0.5)
		 end
		 finish = finish + 1
		 left(face)
		 digWidth(width)
		 if finish == length then
			right(face)
		 end
	  end
   end
   if finish ~= length then
	  if face == 4 then
		 right(face)
		 while turtle.detect() == true do
			turtle.dig()
			addB(blocks)
			sleep(0.5)
		 end
		 turtle.forward()
		 while turtle.detectUp() == true do
			turtle.digUp()
			addB(blocks)
			sleep(0.5)
		 end
		 while turtle.detectDown() == true do
			turtle.digDown()
			addB(blocks)
			sleep(0.5)
		 end
		 finish = finish + 1
		 right(face)
		 digWidth()
		 if finish == length then
			left(face)
			left(face)
			walkWidth(width)
			right(face)
		 end
	  end
   end
end


Again, any help or guidance would be much appreciated.
MindenCucc #2
Posted 22 March 2013 - 06:25 AM
Hello!
I don't know really, what you want, but try this:


count=0
write("Width: ")
width=io.read()
write("Altitude: ")
alt=io.read()
mineFlat(width, alt)
function mineFlat(width, alt)
  print(width*alt.." blocks mined")
  for asd=1,alt do
  turtle.refuel(1)
  turtle.digDown()
  turtle.down()
	for asdx=1,width-1 do
	  turtle.dig()
	  turtle.forward()
	  count=count+1
	end
  turtle.turnLeft()
  turtle.turnLeft()
  end
end

Place a turtle on the ground


Put fuel into the first slot. Hope it will work :)/>
Ray_Anor #3
Posted 22 March 2013 - 07:55 AM
Try this… look at some errors

--Declare locals.
local tArgs = {...}
local length, width, depth
if #tArgs>1 then
length = tonumber(tArgs[1])
width = tonumber(tArgs[2])
if length==0 or width==0 or length==nil or width==nil then
  print("invalid arguments!")
  return -1
end
end
if #tArgs>2 then
depth = tonumber(tArgs[3])
if depth==0 or depth==nil then
  print("invalid depth, working on depth 1")
  depth=1
end
else
depth=1
end
local face = 1
local blocks = 0
local finish = 0

--Create functions.
function left()
turtle.turnLeft()
face = face - 1
if face < 1 then face = face + 4 end
end
function right()
   turtle.turnRight()
   face = face + 1
   if face > 4 then face = face - 4 end
end
function addB()
   blocks = blocks + 1
   if blocks > 1 or blocks==0 then
  print ("Mined "..blocks.." blocks.")
   elseif blocks == 1 then
  print ("Mined "..blocks.." block.")
   end
end
function digFwd(distance)
local trys
for a = 1, distance do
  trys=0
  while not turtle.forward() do
   while turtle.detect() do
    if turtle.dig() then addB() end
   end
   os.sleep(0.25)
   trys=trys+1
   if trys>1000 then
    print("Process interrupted. Can't move forward too long. Check fuel if you don't found a block in front of turtle.")
    return false
   end
  end
  while turtle.detectUp() do
   if turtle.digUp() then addB() end
  end
  while turtle.detectDown() do
   if turtle.digDown() then addB() end
  end
end
return true
end
function walkForward(distance)
local trys
for dNCT = 1, distance do
  while not turtle.forward() do
   os.sleep(0.25)
   trys=trys+1
   if trys>1000 then
    print("Process interrupted. Can't move forward too long. Check fuel if you don't found a block in front of turtle.")
    return false
   end
  end
end
return true
end
function walkDown(distance)
local trys
for dNCT = 1, distance do
  while not turtle.down() do
   os.sleep(0.25)
   trys=trys+1
   if trys>1000 then
    print("Process interrupted. Can't move forward too long. Check fuel if you don't found a block in front of turtle.")
    return false
   end
  end
end
return true
end
function walkUp(distance)
local trys
for dNCT = 1, distance do
  while not turtle.up() do
   os.sleep(0.25)
   trys=trys+1
   if trys>1000 then
    print("Process interrupted. Can't move forward too long. Check fuel if you don't found a block in front of turtle.")
    return false
   end
  end
end
return true
end
--Execute code.
if depth==nil then --do only one time
if not digFwd(1) then
  return blocks, false
end
for i=1,length-2 do
  if not digFwd(width-1) then
   return blocks, false
  end
  right()
  if not digFwd(1) then
   return blocks, false
  end
  right()
end
else
for j=1,depth do
  if not digFwd(1) then
   return blocks, false
  end
  for i=1,length-2 do
   if not digFwd(width-1) then
    return blocks, false
   end
   right()
   if not digFwd(1) then
    return blocks, false
   end
   right()
  end
  right()
  if not walkForward(width-1) then
   return blocks, false
  end
  right()
  if not walkDown(1) then
   return blocks, false
  end
end
end
--[[
while finish ~= length do
   if face == 1 then
		  while turtle.detect() == true do
				 turtle.dig()
				 addB(blocks)
				 sleep(0.5)
		  end
		  turtle.forward()
		  while turtle.detectUp() == true do
				 turtle.digUp()
				 addB(blocks)
				 sleep(0.5)
		  end
		  while turtle.detectDown() == true do
				 turtle.digDown()
				 addB(blocks)
				 sleep(0.5)
		  end
		  finish = finish + 1
		  if width > 0 then
				 right(face)
				 digWidth(width)
				 if finish == length then
					    left(face)
					    left(face)
					    walkWidth(width)
					    right(face)
				 end
		  end
   end
   if finish ~= length then
		  if face == 2 then
				 left(face)
				 while turtle.detect() == true do
					    turtle.dig()
					    addB(blocks)
					    sleep(0.5)
				 end
				 turtle.forward()
				 while turtle.detectUp() == true do
					    turtle.digUp()
					    addB(blocks)
					    sleep(0.5)
				 end
				 while turtle.detectDown() == true do
					    turtle.digDown()
					    addB(blocks)
					    sleep(0.5)
				 end
				 finish = finish + 1
				 left(face)
				 digWidth(width)
				 if finish == length then
					    right(face)
				 end
		  end
   end
   if finish ~= length then
		  if face == 4 then
				 right(face)
				 while turtle.detect() == true do
					    turtle.dig()
					    addB(blocks)
					    sleep(0.5)
				 end
				 turtle.forward()
				 while turtle.detectUp() == true do
					    turtle.digUp()
					    addB(blocks)
					    sleep(0.5)
				 end
				 while turtle.detectDown() == true do
					    turtle.digDown()
					    addB(blocks)
					    sleep(0.5)
				 end
				 finish = finish + 1
				 right(face)
				 digWidth()
				 if finish == length then
					    left(face)
					    left(face)
					    walkWidth(width)
					    right(face)
				 end
		  end
   end
end
]]--
Ikimiko #4
Posted 22 March 2013 - 10:08 AM
Thanks for the codes guys, I'll mess around with them to understand some things that work :)/> If anyone else has more ideas on this code I would love to learn more.