CCCPaste

Stripminer2.0+

-- Strip Mining Program V2.0.1 --
-- Includes anti-gravel protocol --
-- Mines until full, then terminates --
-- Connects to central monitor to relay inventory information --
-- Version 2 introduces a more automated algorithm, allowing for deposition of items once full and returning to mining

-- Tunnels are always two wide, 3 high in current version -- 
-- use "label set MiningTurtleX" to name turtles, and set "chn" to the same number
-- pastebin get Cfdm64bB mine

-- Config -- 
ver = "V2.0.1"
Mside = "left" -- set to whatever side the modem is on the turtle
chn = 1 -- set to channel you want to transmit on





-- Defining tables and variables --

local ores = {
	["minecraft:diamond_ore"]=true,
	["minecraft:emerald_ore"]=true,
	["minecraft:emerald_ore"]=true,
	["minecraft:gold_ore"]=true,
	["minecraft:iron_ore"]=true,
	["minecraft:redstone_ore"]=true,
	["minecraft:lapis_ore"]=true,
	["minecraft:coal_ore"]=true,
	["ic2:blockmetal"]=true,
	["galacticraftcore:basic_block_core"] = true
}

local gravel = {
	["minecraft:gravel"]=true
}

local chest = 0
local inv = {}
local Cscan = {}
local logFile = fs.open("minelog", "a")


-- Assinging functions --

function log(data) -- function to write data into minelog file
	logFile.writeLine(data)
end


function minenup(repeats) -- turtle moving up then mining
cl()
print("Mining...")
	for i = 1,repeats do
		turtle.up()
		turtle.dig()
		scan()
		cl()
	print("Mining...")
	end
end

function minend(repeats) -- turtle moving down then mining
cl()
print("Mining...")
	for l = 1,repeats do
		turtle.down()
		turtle.dig()
		scan()
		cl()
	print("Mining...")
	end
end

function scan() -- scanning blocks around turtle
	cl()
	print("Scanning surroundings...")
	
	success, Cscan = turtle.inspectUp()
	if ores[Cscan.name] then
		turtle.digUp()
		log(Cscan.name)
	end
	
	success, Cscan = turtle.inspectDown()
	if ores[Cscan.name] then
		turtle.digDown()
		log(Cscan.name)
	end
			
	
	for i = 1,4 do
		success, Cscan = turtle.inspect()
		if ores[Cscan.name] then 
			log(Cscan.name)
			turtle.dig()
			turtle.turnRight()
		elseif gravel[Cscan.name] then
			while gravel[Cscan.name] do
				turtle.dig()
				success, Cscan = turtle.inspect()
			end
			turtle.turnRight()
		else
			turtle.turnRight()
		end
	end
end

function mineLayer()
	cl()
	print("Mining...")
	for k = 1,2 do
		readInv()
		scan()
		turtle.dig()
		minenup(2)
		turtle.turnLeft()
		turtle.forward()
		turtle.turnRight()
		turtle.dig()
		scan()
		minend(2)
		turtle.forward()
		turtle.turnRight()
		scan()
		turtle.forward()
		turtle.turnLeft()
		chest = chest + 1
		
	end
end

function cl()
	term.clear()
	term.setCursorPos(1,1)
end

function writeFull() -- obsolete
	local startup = fs.open("startup", "w")
	startup.writeLine("term.clear()")
	startup.writeLine("term.setCursorPos(1,1)")
	startup.writeLine("_G.printError('Inventory full, terminating program...')")
	startup.close()
end

function inList(str, list)
	for _,row in ipairs(list) do
		if row[1] == str then
			return true
		end
	end
	return false
end



function readInv() 
	cl()
	print("Reading inventory...")
	sleep(0.5)
	local inv = {}
	table.insert(inv, {"Slot", "Name", "Count"})
	turtle.select(1)
	
	for i = 1,16 do
		turtle.select(i)
		local detail = turtle.getItemDetail(i, true)
		if detail then
			table.insert (inv, {tostring(i), detail.name, tostring(detail.count)})
		end
	end
	turtle.select(1)
	trans(inv)
	check = inList("16", inv)
	if check == true then
		rtb()
	end
end		


function rtb() -- function to return to start and deposit, then go back and carry on
	cl()
	_G.printError("Inventory full, depositing...")
	turtle.turnRight()
	turtle.turnRight()
	for i = 1,chest do
		turtle.forward()
	end
	for i = 1,16 do 
		turtle.select(i)
		turtle.dropDown()
	end
	turtle.select(1)
	turtle.turnRight()
	turtle.turnRight()
	readInv()
	for i = 0,chest do
		turtle.forward()
	end
end



-- Rednet -- 

modem = peripheral.wrap(Mside)
open =  rednet.isOpen()
modem.open(chn)

if open == false then
	cl()
	print("Opening modem...")
	rednet.open(Mside)
	sleep(0.5)
end

function trans(msg)
	cl()
	print("Transmitting to base on channel " .. chn .. "...")
	modem.transmit(chn, 999, msg)
	sleep(0.5)
end







-- Start of program --

cl()

print("Initialising strip mining program " .. ver .. "...")

sleep(1)

-- Main WHILE loop --

repeat
	mineLayer()
until check == true

-- Main WHILE loop --

-- TESTING AREA -- 




if logFile then
	logFile.close() -- closing log file, if still open
end