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

I am having a bit of trouble with my mining turtle script.

Started by randomman420, 02 January 2013 - 05:36 AM
randomman420 #1
Posted 02 January 2013 - 06:36 AM
Hi, I wrote a script that makes the turtle dig 52 blocks down, placing ladders all the way down, then dig a 5x5x4 room. These numbers are all defaults in case tArgs is empty, all of them can be modified by the user. My problem is that when the turtle digs the room at the bottom of the shaft, it is digging an extra row of blocks out of the bottom of the back wall and an extra block out of the bottom of the left wall. Here is a picture in case I am not being clear enough:

Spoiler

I am 99% sure the problem is with my gravel check in the "smart forward" function I named "fwd()". I just cannot figure out how to make it work without removing the gravel check completely, which would bork my program even worse.

Here is my code, I am pretty new at this, been coding less than a month and never did anything before ComputerCraft, so I am sure it is messy. Any other tips on improving it are welcome and much appreciated, but I really want to figure out how to make the problem described above disappear, as it has been driving me nuts for two days.


Spoiler

--Variables

local tArgs = { ... }			
local height = tArgs[1]		  
local depth = tArgs[2]			
local width = tArgs[3]
local ylevel = tArgs[4]		  

--Functions

local function burrow()
for u = 1,  tonumber(ylevel)  do
  turtle.digDown()
  turtle.down()
  turtle.select(1)
  turtle.placeUp()
end
end

local function fwd()				 --I think this function is where the problem is.
while not turtle.forward() do							  
  if turtle.detect() then								  
   turtle.dig()											
  end													  
end														
end														

--

local function up()
turtle.digUp()
turtle.up()
end

--

local function left()
turtle.turnLeft()
turtle.dig()
fwd()
turtle.turnRight()
end

--

local function down()
for z = 1, height - 1 do
  turtle.digDown()
  turtle.down()
end
end

--

local function column()
for a = 1, height - 1 do
  up()
  end
down()
end

--

local function row()
for b = 1, depth do
   column()
   fwd()
end
end

--

local function comeHome()
for y = 1, depth do
  fwd()
end
end

--

local function right()
turtle.turnRight()
turtle.dig()
fwd()
turtle.turnRight()
end
--

local function wide()
for c = 1, width do
  row()
  turtle.turnLeft()
  turtle.turnLeft()
  comeHome()
  right()
end
end


--Meat and Potatoes

if (tArgs[1] == nil) then
height = 4
end

if (tArgs[2] == nil) then
depth = 5
end

if (tArgs[3] == nil) then
width = 5
end

if (tArgs[4] == nil) then
ylevel = 52
end

burrow()
fwd()
turtle.digUp()
fwd()
wide()


nqeron #2
Posted 02 January 2013 - 12:59 PM
Yeah - testing this out in my own world reveals that it's doing an extra fwd() at the end of a column. A simple fix would be to add a conditional to skip over this in the last iteration of this for loop.

As an aside - generally, if you can you should watch your turtle as it operates, as an aid to debug. Watching the sequence of steps is usually quite informative.
ChunLing #3
Posted 02 January 2013 - 03:41 PM
fwd isn't the problem. It's in the structure of row and wide. In row you repeat a column then fwd, which makes an L if you do it just once. Repeating it gives you overlapping bottoms so you think you're getting a llllL, but really they're all L's. Did that make any kind of sense? Anyway, wide has the same problem, with the right at the end producing a knob sticking out orthogonally from the corner of a rectangle. No letter to represent that, but the same thing happens, you think you've got a stack of rectangles and you forget that each one has a knob sticking into the next until the last sticks its knob in the wall.

There are ways to "fix" this, but that's a kludge. Pat yourself on the back for a working program, then go back and completely revise your movement plan, because what you've got now is pretty inefficient.
randomman420 #4
Posted 02 January 2013 - 04:02 PM
Thanks guys! My problem has been solved. The new and improved (working) code is here:


Spoiler


--Variables

local tArgs = { ... }		     
local height = tArgs[1] or 4		   
local depth = tArgs[2] or 5		  
local width = tArgs[3] or 5
local ylevel = tArgs[4] or 52  

--Functions

local function burrow()
 for u = 1,  tonumber(ylevel)  do
  turtle.digDown()
  turtle.down()
  turtle.select(1)
  turtle.placeUp()
 end
end

local function fwd()			     
 while not turtle.forward() do							  
  if turtle.detect() then								   
   turtle.dig()										     
  end													   
 end													    
end													     

--

local function up()
 turtle.digUp()
 turtle.up()
end
 
--
 
local function left()
 turtle.turnLeft()
 turtle.dig()
 fwd()
 turtle.turnRight()
end

--

local function down()
 for z = 1, height - 1 do
  turtle.digDown()
  turtle.down()
 end
end
 
--

local function column()
 for a = 1, height - 1 do
  up()
  end
fwd()
down()
 end

 --
 
local function row()
 for b = 1, depth - 1 do
   column()
 end
end
 
--

local function comeHome()
 for y = 1, depth - 1 do
 fwd()
 end
end
 
--

local function right()
 turtle.turnRight()
 turtle.dig()
 fwd()
 turtle.turnRight()
end
--

local function wide()
 for c = 1, width do
  row()
  turtle.turnLeft()
  turtle.turnLeft()
  comeHome()
  right()
 end
end

--Meat and Potatoes

burrow()
fwd()
turtle.digUp()
fwd()
wide()


ChunLing #5
Posted 02 January 2013 - 04:23 PM
Hmm…that works. You could probably apply a similar fix to wide.