Turtle Mining Program (ComputerCraft)

This is the source code for a Turtle in ComputerCraft written in Lua. The Turtle will send messages to a receiving computer that has an attached modem. I have modified the code and written code for the receiving computer.

p.s.
I have changed my nickname to ~Black on a friend’s server.

Turtle

-- First set up computer #1.
-- Create a program. Type in the following
-- for i = 1,100 do
-- rednet.open("right")
-- id, message = rednet.receive()
-- print ("Computer ".. id .. " has sent us a message")
-- print ("The message is")
-- print (message)
-- end

isWireless = true
consoleID = 11
depth = 0

function fuel()
	if turtle.getFuelLevel() <= depth + 10 then
		turtle.select(16)
		turtle.refuel(1)
		if isWireless then rednet.send(consoleID,"Refueled. Fuel level: "..turtle.getFuelLevel()) end
	end
end

function isValuable() 
	if turtle.detect() == false then 
		return false 
	end 
	for i=1,3 do 
		turtle.select(i) 
		if turtle.compare() then 
			return false 
		end 
	end 
	return true 
end

function checkWalls(dp)
	for j=1,4 do
		if isValuable() then
			if isWireless then rednet.send(consoleID,"Found ore at depth "..dp) end
			turtle.dig()
		end
		turtle.turnRight()
	end
end

------ ( Program Start ) ------

if isWireless == true then
   rednet.open("right")
end

term.clear()
term.setCursorPos(1,1)
print("The Trololol Miner")
print("-------------------------")

term.write("Start Mining? (y/n): ")

while read() == "y" do

	depth = 0
	print("Commencing mining.")
	if isWireless then rednet.send(consoleID,"Commencing mining.") end

	fuel()
	turtle.digDown()
	for st=1,2 do
		turtle.down()
		depth = depth + 1
		turtle.digDown()
	end

	-- plug entrance hole
	turtle.select(15)
	turtle.placeUp()

	while not turtle.detectDown() do
		fuel()
		turtle.down()
		depth = depth + 1
		if isWireless and depth%10==0 then
			rednet.send(consoleID,"At depth "..depth)
		end
		checkWalls(depth)
		turtle.digDown()
	end

	if isWireless then rednet.send(consoleID,"Moving to next shaft location...") end

	for mv=1,6 do
		fuel()
		turtle.up()
		depth = depth - 1
	end

	-- move forward 2 blocks
	for z=1,2 do
		fuel()
		while not turtle.forward() do
			turtle.dig()
			sleep(.8)
		end
	end

	-- turn right and move one block
	turtle.turnRight()
	fuel()
	while not turtle.forward() do
		turtle.dig()
		sleep(.8)
	end
	turtle.turnLeft()

	-- go down to bedrock
	turtle.digDown()
	while not turtle.detectDown() do
		fuel()
		turtle.down()
		depth = depth + 1
		turtle.digDown()
	end

	if isWireless then rednet.send(consoleID,"Returning to surface!") end

	for k=depth,3,-1 do
		checkWalls(k)
		turtle.digUp()
		fuel()
		while not turtle.up() do
			turtle.digUp()
			sleep(.5)
		end
		if isWireless and k%10 == 0 then
			rednet.send(consoleID,"At depth "..k)
		end
	end

	fuel()
	turtle.digUp()
	turtle.up()
	turtle.digUp()
	turtle.up()

	-- fill exit hole
	turtle.select(15)
	turtle.placeDown()

	turtle.forward()
	turtle.forward()
	turtle.turnRight()
	turtle.forward()
	turtle.turnLeft()

	if isWireless then rednet.send(consoleID,"At the surface!") end
	term.write("Go on mining run? (y/n): ")
end

print("Cancelled mining.")

if isWireless then rednet.close("right") end

Computer

-- First set up computer #1.
-- Create a program. Type in the following
 for i = 1,200 do
 rednet.open("right")
 id, message = rednet.receive()
 print ("Computer ".. id .. " has sent us a message:")
print (message)
print("-------------------------")
end

One thought on “Turtle Mining Program (ComputerCraft)

Leave a Reply

Your email address will not be published. Required fields are marked *