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

Weird behaviour in turtle mining program

Started by stabby, 12 December 2013 - 03:07 AM
stabby #1
Posted 12 December 2013 - 04:07 AM
So basically i've written a turtle mining program to my friends. What its supposed to do is:

Branch mine a 1x2 tunnel as long as the player wants (Argument: Length)
Place torches every 8th block
Place a chest in the end of every tunnel with the mined ores/blocks from that run (Argument: chest)
do as many branch mines as the player wants (Argument: Unit)

So I'm not a pro in Lua or ComputerCraft but i do have some experience in programming in generall.

What's the problem?.. You might ask.

I have no idea! The turtle have kinda its own life. It works great until it get to the second tunnel then when it tries to do the home function it stops about 7-8 blocks before it should and the tunnel system gets messy.

Anyway here's the code. In would appreciate any kind of help!


local tArgs = { ... }
Length = tonumber(tArgs[1])
unit = tonumber(tArgs[2])
chest = tArgs[3]
if Length == nil then
		Length = 5
end
blocksMoved = 0
torchBlocks = 0


function refuel()
		turtle.select(1)
		if turtle.refuel(0) then
				turtle.refuel(1)
		end
end


function mine()
		if turtle.detect() then
				repeat
						turtle.dig()
						sleep(0.5)
				until turtle.detect() == false
		end
		if turtle.detectUp() then
				repeat
						turtle.digUp()
						sleep(0.5)
				until turtle.detectUp() == false
		end
		turtle.forward()
			  
end


function torch()	  
		turtle.turnRight()
		turtle.turnRight()
		turtle.select(2)
		turtle.place()
		turtle.turnLeft()
		turtle.turnLeft()
		torchBlocks = 0
end


function home()
		turtle.digUp()
		turtle.up()
		for i = 1, blocksMoved do
				turtle.back()
				sleep(0.1)
		end
		turtle.down()
		blocksMoved = 0
end

function Chest()
		turtle.turnRight()
		turtle.turnRight()
		if turtle.detect() then
				repeat
						turtle.dig()
						sleep(0.5)
				until turtle.detect() == false
		end
		if turtle.detectUp() then
				repeat
						turtle.digUp()
						sleep(0.5)
				until turtle.detectUp() == false
		end
		turtle.forward()
		if turtle.detectUp() then
				repeat
						turtle.digUp()
						sleep(0.5)
				until turtle.detectUp() == false
		end
		turtle.back()
		turtle.select(3)
		turtle.place()
		for i = 4, 16 do
				turtle.select(i)
				turtle.drop()
		end
		turtle.turnRight()
		turtle.turnRight()
end

function repeats()
		turtle.turnLeft()
		mine()
		mine()
		mine()
		turtle.turnRight()
	  
end

function main()
		for i=1, Length do
				if turtle.getFuelLevel() < 10 then
						refuel()
				end
				mine()
				blocksMoved = blocksMoved + 1
				torchBlocks = torchBlocks + 1
				print(blocksMoved)
				if torchBlocks == 8 then
						torch()
				end
	  
end
		home()
		if chest == "y" then
				Chest()
		end
		if unit > 0 then
				repeats()
				print(blocksMoved)
		end
end
while unit >= 0 do
unit = unit -1
main()
end
Edited on 12 December 2013 - 03:08 AM
Buho #2
Posted 12 December 2013 - 08:57 AM
I'm just parsing your code in-head, but I see something fishy with home(). This may fail with gravel. Also, there's no fuel check for the return. Very likely you will have 90 fuel or less at the end of Length. You will travel backwards until out of fuel, then blocksMoved = 0.

With experience, I've gotten lax about calculating fuel requirements. I just check for a minimum at the beginning of the program, like if it's greater than 10,000 (2 stacks of coal) then continue else err out. Otherwise you have to micromanage your fuel consumption and checking. (Granted, I'm at the "endgame" stage of my world and have chestfulls of coal, charcoal, and lava buckets.)

FYI, you can replace if torchBlocks == 8 with if blocksMoved % 8 == 0 and get rid of a variable.
Buho #3
Posted 12 December 2013 - 09:02 AM
If you want to micromanage your fuel, I suggest writing your own movement functions, such as this:


function back(dist)
    if dist == nil then dist = 1 end
    if turtle.getFuelLevel() < dist then refuel() end
    for i = 1, dist do
        while not turtle.back() do 
            os.sleep(10) --Will wait if there's an obstacle, can add code to deal with that here if you want
        end
    end
end
awsmazinggenius #4
Posted 12 December 2013 - 09:07 AM
If you want to micromanage your fuel, I suggest writing your own movement functions, such as this:


function back(dist)
	if dist == nil then dist = 1 end
	if turtle.getFuelLevel() < dist then refuel() end
	for i = 1, dist do
		while not turtle.back() do
			os.sleep(10) --Will wait if there's an obstacle, can add code to deal with that here if you want
		end
	end
end
10 seconds is a little much, and even that won't stop gravel in the path. You would need to turn twice, then dig().
Buho #5
Posted 12 December 2013 - 09:11 AM
Yeah, it'll just hang until someone rescues the turtle, similar to the built-in "go" program.
stabby #6
Posted 12 December 2013 - 10:33 AM
Thanks guys!

It was the fuel problem.. I can't believe I didn't think of that.

Works perfect now :)/>