CCCPaste

botminer BACKUP 2023

--pastebin get q354743w botminer
--Last Update: 03.04.2023-20:27

term.clear()
term.setCursorPos(1,1)

local ignore = {"minecraft:stone",
				"minecraft:dirt",
				"minecraft:andesite",
				"minecraft:granite",
				"minecraft:diorite",
				"minecraft:gravel",
				"minecraft:deepslate",
				"minecraft:cobbled_deepslate",
				"minecraft:tuff",
				"minecraft:calcite",
				"minecraft:smooth_basalt",
				"minecraft:amethyst_block",
				"soul_ice:lightstone",
				"quark:limestone",
				"create:weathered_limestone",
				"create:limestone",
				"create:dolomite",
				"create:gabbro",
				"create:natural_scoria",
				"create:dark_scoria",
				"create:scoria",
				"appliedenergistics2:charged_quartz_ore",
				"minecraft:obsidian",
				"minecraft:sand",
				"minecraft:cobblestone",	
				"minecraft:grass_block",
				"minecraft:bedrock",
				"minecraft:oak_planks",
				"minecraft:chest",
				"minecraft:torch",					
				"computercraft:turtle_advanced",
				"computercraft:turtle_normal"}

local backupignore = {"minecraft:stone",
					"minecraft:chest",
					"minecraft:torch",
					"minecraft:cobblestone",
					"minecraft:sand",
					"minecraft:grass_block",
					"minecraft:dirt",
					"minecraft:gravel",
					"minecraft:obsidian",
					"minecraft:andesite",
					"minecraft:granite",
					"minecraft:diorite",
					"minecraft:bedrock",
					"computercraft:turtle_advanced",
					"computercraft:turtle_normal",
					"quark:marble",
					"quark:limestone",
					"quark:slate",
					"quark:deepslate",
					"forbidden_arcanus:dark_stone",
					"forbidden_arcanus:petrified_root",
					"silentgems:hardened_netherrack",
					"silentgems:hardened_end_stone",
					"create:weathered_limestone",
					"create:limestone",
					"create:dolomite",
					"create:gabbro",
					"create:natural_scoria",
					"create:dark_scoria",
					"create:scoria",
					"astralsorcery:marble_raw",
					"chisel:basalt2",
					"rustic:slate",
					"chisel:limestone2",
					"chisel:marble2",
					"chisel:stonebrick",
					"chisel:stonebrick1",
					"chisel:stonebrick2",
					"chisel:cobblestone"}


local modemFound = false
local programReset = false
local programInspect = false
local returnedStep = false
local skipShafts = false
local cancelProgram = false
local oreFound = false
local maxOreLimitHit = false

local posDforward = 0
local shaft = 0
local skipShaftsNum = 0
local amountOreTotal = 0
local startShaft = 1
local maxOres = 100 --max amount of ores in one vein; if reached bot will stop vein mining and return to shaft, at next drop off at chest bot will display message that the limit was hit.

local startTime = 0
local endTime = 0
local differenceTime = 0
local firstDropSlot = 1

local foundOre=""
local BotStatus = "Booting"

local monitorMessage =""

local posX = 0
local posY = 0
local posZ = 0
local posD = 0

local posXhome = 0
local posYhome = 0
local posZhome = 0
local posDhome = 0

local posXsave = {}
local posYsave = {}
local posZsave = {}
local posDsave = {}

local posXgps = 0
local posYgps = 0
local posZgps = 0
local posDgps = 0

local posXgps2 = 0
local posYgps2 = 0
local posZgps2 = 0

local posXgpshome = 0
local posYgpshome = 0
local posZgpshome = 0
local posDgpshome = 0

--Filnames: Config,DisplayID,GPS,DisplaySave
local filenames={"bmconfig","bmsave","bmgps","bmdispsave"}

local statusText = {"Running",  		--1
					"Need Fuel",		--2
					"Mining Vein", 		--3		+oreamount
					"Return Vein",		--4
					"Heading Home",		--5
					"Leaving Home", 	--6		after heading home
					"Deposit Stuff",	--7
					"Moved GPS-Home",	--8
					"At GPS-Home",		--9
					"No GPS!",			--10
					"Chest Full",		--11
					"Max Ore Limit",	--12
					"Chest Full"}		--13
local arg={...}
local saveTable = {}
local gpsTable = {}


local DisplayHeaderTable = {"BotID : ",
							"Fuel  : ",
							"PosX  : ",
							"PosY  : ",
							"PosZ  : ",
							"PosD  : ",
							"Shaft : ",
							"Status: "}
local DisplayDataTable = {}
local DisplayTable = {}

local configText = {"Length of Shafts",
					"Number of Shafts",
					"Slot for checking if full",
					"Minimum amount of Fuel",
					"Direction for Shafts 1=Right 3=Left",
					"Use GPS? 1=Yes 0=No",
					"Use Ender Chest Drop? 1=Yes 0=No",
					"Use Charged Certus detection? 1=Yes 0=No"}
local config={}
--Default config for 5x5 chunks loaded starting in the middle of the edge chunck
config[1] = 60 		--length of shafts
config[2] = 22   	--num of shafts
config[3] = 13  	--checkslot for full
config[4] = 500 	--minFuel
config[5] = 1		--Shaft direction 1=Right 3=Left
config[6] = 0		--GPS
config[7] = 0		--EnderChestDrop
config[8] = 0		--Charged Ore

function useArgs()
	readParams = 0
	while readParams < #arg do
		if string.lower(arg[readParams+1]) == "-skip" then
			if not (readParams+1 < #arg) or tonumber(readParams+2) == nil  then
				print('Bad Argument! Type "botminer -help" for help!')
				return false
			else
				skipShafts = true
				skipShaftsNum = tonumber(arg[readParams+2])
				readParams=readParams+2
			end
		elseif string.lower(arg[readParams+1]) == "-reset" then
			programReset = true
			readParams=readParams+1
		elseif string.lower(arg[readParams+1]) == "-inspect" then
			programInspect = true
			readParams=readParams+1
		elseif string.lower(arg[readParams+1]) == "-refuel" then
			firstDropSlot = 1
			while true do
				advRefuel()
			end
		elseif string.lower(arg[readParams+1]) == "-help" then
				print("Help-Page:")
				print("botminer -skip 3 -> Amount of shafts the bot will skip.")
				print("botminer -reset -> Will reset the config of the bot.")
				print("botminer -inspect -> Shows block name in front of bot.")
				return false
		else
			print('Bad Argument! Type "botminer -help" for help!')
			return false
		end
	end
	return true
end

function GPSfile()
	if not fs.exists(filenames[3]) then
		gpsTable = {posXgps, posYgps, posZgps, posDgps}
		f = fs.open(filenames[3], "w")
		f.write(textutils.serialize(gpsTable))
		f.close()
		createStartup(true)
		return false
	else
		f = fs.open(filenames[3], "r")
		gpsTable =textutils.unserialize(f.readAll())
		f.close()
		posXgpshome = gpsTable[1]
		posYgpshome = gpsTable[2]
		posZgpshome = gpsTable[3]
		posDgpshome = gpsTable[4]
		return true
	end
end

function DisplaySavefile()
	if not fs.exists(filenames[4]) then
		createStartup(true)
		return false
	else
		f = fs.open(filenames[4], "r")
		DisplayDataTable = textutils.unserialize(f.readAll())
		f.close()
		return true
	end
end

function bot_getGPSpos()
	posXgps, posYgps, posZgps = gps.locate()
	if not posXgps then return false end
	while turtle.forward() == false do
		turtle.dig()
		turtle.attack()
	end
	posXgps2, posYgps2, posZgps2 = gps.locate()
	if 	   posXgps < posXgps2 then 
		posDgps = 0 
	elseif posYgps < posYgps2 then 
		posDgps = 1 
	elseif posXgps > posXgps2 then 
		posDgps = 2 
	elseif posYgps > posYgps2 then 
		posDgps = 3 
	end
	while turtle.back() == false do 
		bot_turnRight()
		bot_turnRight()
		turtle.dig()
		turtle.attack()
		bot_turnRight()
		bot_turnRight()
	end
	return true
end

function bot_checkGPShome()
	if posXgps == posXgpshome then 
		if posYgps == posYgpshome then 
			if posZgps == posZgpshome then 
				if posDgps == posDgpshome then 
					return true
				else 
					return false
				end
			else 
				return false
			end
		else 
			return false
		end
	else 
		return false
	end
end

function bot_returnhomeGPS()
	
	posX = posXgps
	posY = posYgps
	posZ = posZgps
	posD = posDgps
	local calcXgps = posXgps - posXgpshome
	local calcYgps = posYgps - posYgpshome

	while posZ ~= posZgpshome do
		if posZ > posZgpshome then 
			bot_down()
		elseif posZ < posZgpshome then
			bot_up()
		end
	end
	if calcXgps <= calcYgps then
		if posX > posXgpshome then
			while posD ~= 2 do
				bot_turnRight()
			end
			while posX ~= posXgpshome do
				bot_forward()
			end
		elseif posX < posXgpshome then
			while posD ~= 0 do
				bot_turnRight()
			end
			while posX ~= posXgpshome do
				bot_forward()
			end
		end
		if posY > posYgpshome then
			while posD ~= 3 do
				bot_turnRight()
			end
			while posY ~= posYgpshome do
				bot_forward()
			end
		elseif posY < posYgpshome then
			while posD ~= 1 do
				bot_turnRight()
			end
			while posY ~= posYgpshome do
				bot_forward()
			end
		end
	elseif calcYgps <= calcXgps then
		if posY > posYgpshome then
			while posD ~= 3 do
				bot_turnRight()
			end
			while posY ~= posYgpshome do
				bot_forward()
			end
		elseif posY < posYgpshome then
			while posD ~= 1 do
				bot_turnRight()
			end
			while posY ~= posYgpshome do
				bot_forward()
			end
		end
		if posX > posXgpshome then
			while posD ~= 2 do
				bot_turnRight()
			end
			while posX ~= posXgpshome do
				bot_forward()
			end
		elseif posX < posXgpshome then
			while posD ~= 0 do
				bot_turnRight()
			end
			while posX ~= posXgpshome do
				bot_forward()
			end
		end	
	end
	while posD ~= posDgpshome do
		bot_turnRight()
	end
	
	posX = 0
	posY = 0
	posZ = 0
	posD = 0
end

function configFile()
	if not fs.exists(filenames[1]) then
		configInput()
		f = fs.open(filenames[1], "w")
		f.write(textutils.serialize(config))
		f.close()
	else
		f = fs.open(filenames[1], "r")
		config = textutils.unserialize(f.readAll())
		f.close()
	end
	for i=1, #config do
		config[i] = tonumber(config[i])
	end
end

function configInput()
	for i=1, #config do
		local IOinput = ""
		term.clear()
		term.setCursorPos(1,1)
		print("Default is for 5x5 chunks loaded;")
		print("starting at middle of edge chunk.")
		print(configText[i].." - Default: "..config[i])
		IOinput = tonumber(io.read())
		if type( IOinput ) == "number" then
			config[i] = IOinput
			print("Used input: "..config[i])
		else
			print("Used default value!")
		end
		sleep(1)
	end
	configCheck()
end

function configCheck()
	term.clear()
	term.setCursorPos(1,1)
	if config[1] < 1 then									--length
		print("Wrong Input at: "..configText[1].." Press ENTER!")
		io.read()
		configInput()
	elseif config[2] < 1 or config[2] > 99 then   			--shafts
		print("Wrong Input at: "..configText[2].." Press ENTER!")
		io.read()
		configInput()
	elseif config[3] < 1 or config[3] > 16 then 			--checkslot
		print("Wrong Input at: "..configText[3].." Press ENTER!")
		io.read()
		configInput()
	elseif config[4] < 1 then 								--minFuel
		print("Wrong Input at: "..configText[4].." Press ENTER!")
		io.read()
		configInput()
	elseif config[5] ~= 1 and config[5] ~= 3 then			--Shaft direction
		print("Wrong Input at: "..configText[5].." Press ENTER!")
		io.read()
		configInput()
	elseif config[6] ~= 0 and config[6] ~= 1 then			--GPS
		print("Wrong Input at: "..configText[6].." Press ENTER!")
		io.read()
		configInput()
	end
end

function wlanDisplay()
	for _,side in ipairs({"left", "right"}) do
		if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
			rednet.open(side)
			modemFound = true
			-- if not fs.exists(filenames[2]) then
				-- print("WLAN-Display-ID?")
				-- saveTable[1] = tonumber(io.read())
				-- f = fs.open(filenames[2], "w")
				-- f.write(textutils.serialize(saveTable))
				-- f.close()
			-- else
				-- f = fs.open(filenames[2], "r")
				-- saveTable =textutils.unserialize(f.readAll())
				-- f.close()
			-- end
			break
		end
	end
end

function displayUpdate(reboot)
	
	-- DisplayHeaderTable[1] = "BotID : "
	-- DisplayHeaderTable[2] = "Fuel  : "
	-- DisplayHeaderTable[3] = "PosX  : "
	-- DisplayHeaderTable[4] = "PosY  : "
	-- DisplayHeaderTable[5] = "PosZ  : "
	-- DisplayHeaderTable[6] = "PosD  : "
	-- DisplayHeaderTable[7] = "Shaft : "
	-- DisplayHeaderTable[8] = "Status: "
	
	--Prepare and save Display Data
	if reboot == false then 
		DisplayDataTable[1] =  os.getComputerID()
		DisplayDataTable[2] =  turtle.getFuelLevel()
		DisplayDataTable[3] =  posX
		DisplayDataTable[4] =  posY
		DisplayDataTable[5] =  posZ
		DisplayDataTable[6] =  posD
		DisplayDataTable[7] =  shaft
		DisplayDataTable[8] =  BotStatus
		DisplayDataTable[9] =  turtle.getFuelLimit()
		DisplayDataTable[10] = config[2] --num of shafts
	
		if fs.exists(filenames[4]) then fs.delete(filenames[4]) end
		f = fs.open(filenames[4], "w")
		f.write(textutils.serialize(DisplayDataTable))
		f.close()
	end
	
	
	--Display Info in Bot Terminal
	DisplayTable[1] = DisplayHeaderTable[1]..DisplayDataTable[1]
	DisplayTable[2] = DisplayHeaderTable[2]..DisplayDataTable[2].." / "..DisplayDataTable[9]
	DisplayTable[3] = DisplayHeaderTable[3]..DisplayDataTable[3]
	DisplayTable[4] = DisplayHeaderTable[4]..DisplayDataTable[4]
	DisplayTable[5] = DisplayHeaderTable[5]..DisplayDataTable[5]
	DisplayTable[6] = DisplayHeaderTable[6]..DisplayDataTable[6]
	DisplayTable[7] = DisplayHeaderTable[7]..DisplayDataTable[7].."/"..DisplayDataTable[10]
	DisplayTable[8] = DisplayHeaderTable[8]..DisplayDataTable[8] 
	
	term.clear()
	term.setCursorPos(1,1)
	for i=1, #DisplayTable do
		print(DisplayTable[i])
	end
	
	if modemFound then
		modemSend()
		-- modemReceive()
	end
end

function modemSend()
	local dataTable = {	tostring(DisplayDataTable[1]),
						tostring(DisplayDataTable[2]),
						tostring(DisplayDataTable[3]),
						tostring(DisplayDataTable[4]),
						tostring(DisplayDataTable[5]),
						tostring(DisplayDataTable[6]),
						tostring(DisplayDataTable[8]),
						tostring(DisplayDataTable[7]),
						tostring(DisplayDataTable[10])}
	
	--rednet.send(tonumber(saveTable[1]),textutils.serialize(dataTable),"BOT")
	rednet.broadcast(textutils.serialize(dataTable),"BOT")
end

-- function modemReceive()
	-- local senderId, message, protocol = rednet.receive("BOT",0.01)
	-- if senderId == saveTable[1] then
		-- monitorMessage = message
		-- if message == "cancel" then
			-- cancelProgram = true
		-- end
	-- else
		-- monitorMessage = ""
	-- end
-- end

function bot_digTunnel()
	while turtle.forward() == false do
		turtle.dig()
		turtle.attack()
	end
	if posD == 0 then 
		posX = posX+1
	elseif posD == 1 then 
		posY = posY+1
	elseif posD == 2 then 
		posX = posX-1
	elseif posD == 3 then 
		posY = posY-1
	end	
	displayUpdate(false)
	turtle.digUp()
end

function bot_returnTunnel() --WHY!!!!!!!!!!!!!!!!!!!
	while turtle.forward() == false do
		turtle.dig()
		turtle.attack()
	end
	posX = posX-1
	displayUpdate(false)
end

function bot_forward()
	while turtle.forward() == false do
		turtle.dig()
		turtle.attack()
	end
	if posD == 0 then 
		posX = posX+1
	elseif posD == 1 then 
		posY = posY+1
	elseif posD == 2 then 
		posX = posX-1
	elseif posD == 3 then 
		posY = posY-1
	end	
	displayUpdate(false)
end

function bot_back()
	while turtle.back() == false do 
		bot_turnRight()
		bot_turnRight()
		turtle.dig()
		turtle.attack()
		bot_turnRight()
		bot_turnRight()
	end
	if posD == 0 then 
		posX = posX-1
	elseif posD == 1 then 
		posY = posY-1
	elseif posD == 2 then 
		posX = posX+1
	elseif posD == 3 then 
		posY = posY+1
	end	
	displayUpdate(false)
end

function bot_up()
	while turtle.up() == false do
		turtle.digUp()
		turtle.attackUp()
	end
	posZ = posZ+1
	displayUpdate(false)
end

function bot_down()
	while turtle.down() == false do
		turtle.digDown()
		turtle.attackDown()
	end
	posZ = posZ-1
	displayUpdate(false)
end

function bot_turnRight()
	if turtle.turnRight() then
		posD = posD + 1
		if posD > 3 then 
			posD = 0
		end
	end
end

function bot_turnLeft()
	if turtle.turnLeft() then
		posD = posD - 1
		if posD < 0 then 
			posD = 3
		end
	end
end

function bot_checkoresUp()
	if turtle.detectUp() then
		local successUp, dataUp = turtle.inspectUp()
		if successUp then
			for i=1,#ignore do
				if dataUp.name == ignore[i] then
					if dataUp.name == "appliedenergistics2:charged_quartz_ore" then
						chargedOre()
					end
					return false
				end
			end
			foundOre = "Up"
			return true
		end
	end
end

function bot_checkoresDown()
	if turtle.detectDown() then
		local successDown, dataDown = turtle.inspectDown()
		if successDown then
			for i=1,#ignore do
				if dataDown.name == ignore[i] then
					if dataDown.name == "appliedenergistics2:charged_quartz_ore" then
						chargedOre()
					end
					return false
				end
			end
			foundOre = "Down"
			return true
		end
	end
end

function bot_checkoresRight()
	bot_turnRight()
	if turtle.detect() then
		local successRight, dataRight = turtle.inspect()
		if successRight then
			for i=1,#ignore do
				if dataRight.name == ignore[i] then
					if dataRight.name == "appliedenergistics2:charged_quartz_ore" then
						chargedOre()
					end
					bot_turnLeft()
					return false				
				end
			end
			foundOre = "Right"
			bot_turnLeft()
			return true
		end	
	end
	bot_turnLeft()
end

function bot_checkoresLeft()
	bot_turnLeft()
	if turtle.detect() then
		local successLeft, dataLeft = turtle.inspect()
		if successLeft then
			for i=1,#ignore do
				if dataLeft.name == ignore[i] then
					if dataLeft.name == "appliedenergistics2:charged_quartz_ore" then
						chargedOre()
					end
					bot_turnRight()
					return false				
				end
			end
			foundOre = "Left"
			bot_turnRight()
			return true
		end
	end
	bot_turnRight()
end

function bot_checkoresBack()
	bot_turnLeft()
	bot_turnLeft()
	if turtle.detect() then
		local successBack, dataBack = turtle.inspect()
		if successBack then
			for i=1,#ignore do
				if dataBack.name == ignore[i] then
					if dataBack.name == "appliedenergistics2:charged_quartz_ore" then
						chargedOre()
					end
					bot_turnRight()
					bot_turnRight()
					return false				
				end
			end
			foundOre = "Back"
			bot_turnRight()
			bot_turnRight()
			return true
		end
	end
	bot_turnRight()
	bot_turnRight()
end

function bot_checkoresFornt()
	if turtle.detect() then
		local successFront, dataFront = turtle.inspect()
		if successFront then
			for i=1,#ignore do
				if dataFront.name == ignore[i] then
					if dataFront.name == "appliedenergistics2:charged_quartz_ore" then
						chargedOre()
					end
					return false				
				end
			end
			foundOre = "Front"
			return true
		end
	end
end

function chargedOre()
	if config[8] == 1 then
		BotStatus = statusText[13]
		term.clear()
		term.setCursorPos(1,1)
		print("Found Charged Certus Quartz! Press ENTER.")
		io.read()
		BotStatus = statusText[1]
	end
end

function bot_savePos()
	table.insert(posXsave,posX)
	table.insert(posYsave,posY)
	table.insert(posZsave,posZ)
	table.insert(posDsave,posD)
end

function bot_turntoOre()
	if foundOre == "Right" then
		bot_turnRight()
	elseif foundOre == "Left" then
		bot_turnLeft()
	elseif foundOre == "Back" then
		bot_turnLeft()
		bot_turnLeft()
	end
end

function bot_digOre()
	if foundOre == "Up" then
		bot_up()
	elseif foundOre == "Down" then
		bot_down()
	elseif foundOre == "Right" then
		bot_forward()
	elseif foundOre == "Left" then
		bot_forward()
	elseif foundOre == "Front" then
		bot_forward()		
	end
end

function mineOresCheck()
	if returnedStep then
		returnedStep = false
		if bot_checkoresUp() or bot_checkoresDown() or bot_checkoresFornt() or bot_checkoresRight() or bot_checkoresLeft() or bot_checkoresBack() then
			oreFound = true
			return true
		else
			oreFound = false
			return false	
		end
	else
		if bot_checkoresUp() or bot_checkoresDown() or bot_checkoresFornt() or bot_checkoresRight() or bot_checkoresLeft() then
			oreFound = true
			return true
		else
			oreFound = false
			return false	
		end
	end
end

function bot_returnStep()
	--while posX ~= posXsave or posY ~= posYsave or posZ ~= posZsave or posD ~= posDsave do
		local x = #posXsave
		if posZ > posZsave[x] then bot_down() end
		if posZ < posZsave[x] then bot_up() end
		if posD == 0 then
			if posX > posXsave[x] then bot_back() end
			if posX < posXsave[x] then bot_forward() end
		elseif posD == 1 then
			if posY > posYsave[x] then bot_back() end
			if posY < posYsave[x] then bot_forward() end
		elseif posD == 2 then
			if posX < posXsave[x] then bot_back() end
			if posX > posXsave[x] then bot_forward() end	
		elseif posD == 3 then
			if posY < posYsave[x] then bot_back() end
			if posY > posYsave[x] then bot_forward() end	
		end
		while posD ~= posDsave[x] do
			bot_turnRight()
		end
		if posD == 0 then
			if posX > posXsave[x] then bot_back() end
			if posX < posXsave[x] then bot_forward() end
		elseif posD == 1 then
			if posY > posYsave[x] then bot_back() end
			if posY < posYsave[x] then bot_forward() end
		elseif posD == 2 then
			if posX < posXsave[x] then bot_back() end
			if posX > posXsave[x] then bot_forward() end	
		elseif posD == 3 then
			if posY < posYsave[x] then bot_back() end
			if posY > posYsave[x] then bot_forward() end	
		end
	--end
	table.remove(posXsave,x)
	table.remove(posYsave,x)
	table.remove(posZsave,x)
	table.remove(posDsave,x)
end

function bot_mineOres()
	local mineores = true
	local amountOreVein = 0
	while mineores do
		if config[7] == 1 then
			if bot_StatusInventory() then
				bot_dropStuffEnder()
			end
		end
		BotStatus = statusText[3]..": "..amountOreVein
		displayUpdate(false)
		if amountOreVein < maxOres then
			bot_turntoOre()
			bot_savePos()
			bot_digOre()
			amountOreVein = amountOreVein + 1
			displayUpdate(false)
			while mineOresCheck() == false and #posXsave > 0 do
				bot_returnStep()
				returnedStep = true
				BotStatus = statusText[4]..": "..amountOreVein
			end
		else
			while #posXsave > 0 do
				bot_returnStep()
				returnedStep = true
				BotStatus = statusText[4]..": "..amountOreVein
			end
			oreFound = false
			maxOreLimitHit = true
		end
		if #posXsave == 0 and oreFound == false then
			mineores = false
			amountOreTotal = amountOreTotal + amountOreVein
			BotStatus = statusText[1]
			turnDforward(posDforward)
			-- if bot_checkoresRight() or bot_checkoresLeft() then
				-- bot_mineOres() 
			-- end
		end
	end
end

function bot_returnhome()
	BotStatus = statusText[5]
	posXhome = posX
	posYhome = posY
	posZhome = posZ
	posDhome = posD
	
	while posZ ~= 1 do
		if posZ > 1 then 
			bot_down()
		elseif posZ < 1 then
			bot_up()
		end
	end
	while posD ~= 2 do
		bot_turnRight()
	end
	while posX ~= 0 do
		bot_forward()
	end
	if posY ~= 0 then
		if config[5] == 1 then
			while posD ~= 3 do
				bot_turnRight()
			end
		elseif config[5] == 3 then
			while posD ~= 1 do
				bot_turnRight()
			end
		end
		while posY ~= 0 do
			bot_forward()
		end
		while posD ~= 2 do
			bot_turnRight()
		end
	end
	bot_down()
end

function bot_leavehome()
	BotStatus = statusText[6]
	bot_up()
	if posYhome ~= 0 then
		while posD ~= config[5] do
			bot_turnRight()
		end
		while posY ~= posYhome do
			bot_forward()
		end
	end
	while posD ~= 0 do
		bot_turnRight()
	end
	
	while posX ~= posXhome do
		bot_forward()
	end
	while posZ ~= posZhome do
		if posZ > posZhome then 
			bot_down()
		elseif posZ < posZhome then
			bot_up()
		end
	end
	while posD ~= posDhome do
		bot_turnRight()
	end
	posXhome = 0
	posYhome = 0
	posZhome = 0
	posDhome = 0
	BotStatus = statusText[1]
end

function bot_StatusFuel()
	if 	turtle.getFuelLevel() < config[4] then
		return true
	else 
		return false
	end	
end

function bot_StatusInventory()
	if turtle.getItemCount(config[3]) > 0 then
		return true
	else 
		return false
	end	
end

function bot_checkStatus()
	if bot_StatusFuel() then
		bot_home("Fuel")
	elseif bot_StatusInventory() then
		bot_home("Inv")
	elseif cancelProgram then
		bot_returnhome()
		bot_dropStuff()
		programEnd()
	end
end

function bot_dropStuffHome()
	bot_checkMaxOreLimitHit()
	BotStatus = statusText[7]
	displayUpdate(false)
	for i=firstDropSlot,16 do
		turtle.select(i)
		if turtle.getItemCount() > 0 then
			while not turtle.drop() do
				BotStatus = statusText[11]
				displayUpdate(false)
				term.clear()
				term.setCursorPos(1,1)
				print("Empty chest and press ENTER.")
				io.read()
				BotStatus = statusText[7]
				displayUpdate(false)
			end
		end
	end
	turtle.select(firstDropSlot)
	BotStatus = statusText[1]
end

function bot_dropStuffEnder()
	bot_checkMaxOreLimitHit()
	BotStatus = statusText[7]
	displayUpdate(false)
	turtle.dig()
	turtle.select(1)
	turtle.place()
	for i=firstDropSlot,16 do
		turtle.select(i)
		if turtle.getItemCount() > 0 then
			while not turtle.drop() do
				BotStatus = statusText[11]
				displayUpdate(false)
				term.clear()
				term.setCursorPos(1,1)
				print("Empty chest!")
			end
			BotStatus = statusText[7]
			displayUpdate(false)
		end
	end	
	turtle.select(1)
	turtle.dig()
	BotStatus = statusText[1]
end

function bot_checkMaxOreLimitHit()
	if maxOreLimitHit == true then
		BotStatus = statusText[12]
		displayUpdate(false)
		term.clear()
		term.setCursorPos(1,1)
		print("Bot hit max Ore Limit("..maxOres.."Ores) for one vein, maybe there is a new material which needs to be added to the ignore list?")
		print("Press ENTER to resume")
		io.read()
		maxOreLimitHit = false
	end
end

function bot_refuel(minimum)
	BotStatus = statusText[2]
	displayUpdate(false)
	while turtle.getFuelLevel() < minimum do
		advRefuel()
	end
	turtle.select(firstDropSlot)
	BotStatus = statusText[1]
end

function bot_home(reason)
	if config[7] == 0 then
		bot_returnhome()
		bot_dropStuffHome()
		bot_refuel(config[4])
		bot_leavehome()
	else
		if reason == "Fuel" then
			bot_returnhome()
			bot_refuel(config[4])
			bot_leavehome()
		elseif reason == "Inv" then
			bot_dropStuffEnder()
		end
	end
end

function nextShaft()
	bot_up()
	posDforward = config[5]
	turnDforward(posDforward)
	for i=1,3 do
		bot_digTunnel()
		turtle.digDown()
	end
	posDforward = 0
	turnDforward(posDforward)
	bot_down()
end

function skipShaft()
	bot_up()
	posDforward = config[5]
	turnDforward(posDforward)
	for i=1, skipShaftsNum do
		for i=1,3 do
			bot_digTunnel()
			turtle.digDown()
		end
	end
	posDforward = 0
	turnDforward(posDforward)
	bot_down()
end

function programEnd()
	endTime = os.clock()
	differenceTime = endTime - startTime
	differenceTime = math.floor (differenceTime)
	BotStatus = differenceTime.."s / "..amountOreTotal.." Ores"
	if fs.exists(filenames[3]) then fs.delete(filenames[3]) end
	displayUpdate(false)
	print("END")
end

function programStop()
	while true do
		displayUpdate(false)
		print("STOP")
		sleep(5)
	end
end

function programTerminate()
	print("Terminated")
	sleep(1)
	os.reboot()
end

function turnDforward(pos)
	while posD ~= pos do
		if posD == 0 and pos == 1 then 
			bot_turnRight()
		elseif posD == 0 and pos == 3 then 
			bot_turnLeft()
		elseif posD == 1 and pos == 0 then 
			bot_turnLeft()
		elseif posD == 1 and pos == 2 then 
			bot_turnRight()
		elseif posD == 2 and pos == 1 then 
			bot_turnLeft()
		elseif posD == 2 and pos == 3 then 
			bot_turnRight()
		elseif posD == 3 and pos == 2 then 
			bot_turnLeft()
		elseif posD == 3 and pos == 0 then 
			bot_turnRight()
		else
			bot_turnRight()
		end
	end
end

function bot_GPSreboot()
	if bot_getGPSpos() then	
		if GPSfile() then
			if not bot_checkGPShome() then 
				bot_returnhomeGPS()
				createStartup(false)
				if fs.exists(filenames[3]) then fs.delete(filenames[3]) end
				BotStatus = statusText[8]
				programStop()
			end
			createStartup(false)
			if fs.exists(filenames[3]) then fs.delete(filenames[3]) end
			BotStatus = statusText[9]
			programStop()
		else
			main()
		end
	else
		BotStatus = statusText[10]
		print("No GPS found!")
		print("To start without GPS press ENTER.")
		io.read()
		createStartup(false)
		if fs.exists(filenames[3]) then fs.delete(filenames[3]) end
		main()
	end
end

function createStartup(autostart)
	if fs.exists("startup") then fs.delete("startup") end
	L1 = "fs.delete('botminer')"
	L2 = "shell.run('pastebin','get','q354743w','botminer')"
	L3 = "shell.run('botminer')"

	startup = fs.open("startup", "w")
	startup.writeLine(L1)
	startup.writeLine(L2)
	if autostart then
		startup.writeLine(L3)
	end
	startup.close()
end

function advRefuel()
	local neededLavaBuckets = (turtle.getFuelLimit() - turtle.getFuelLevel()) / 1000
	local neededCoal		= (turtle.getFuelLimit() - turtle.getFuelLevel()) / 80
	local neededCoalStacks  = neededCoal / 64
	turtle.select(firstDropSlot)
	term.clear()
	term.setCursorPos(1,1)
	print("Refuel Overview")
	print("Fuel Level: "..turtle.getFuelLevel().." / "..turtle.getFuelLimit())
	print("-------------------------------------")
	print("Needed Material for full refuel:     ")
	print("Lava Buckets   : ".. neededLavaBuckets)
	print("Coal / Charcoal: ".. neededCoal.." ("..neededCoalStacks.." Stacks)")
	print("-------------------------------------")
	print("Put fuel in any slot and press ENTER.")
	io.read()
	for i=firstDropSlot, 16 do
		if turtle.getFuelLevel() >= turtle.getFuelLimit() then
			break
		end
		turtle.select(i)
		turtle.refuel()
		term.clear()
		term.setCursorPos(1,1)
		print("Fuel: "..turtle.getFuelLevel().." / "..turtle.getFuelLimit())
	end
	print("Remove Buckets and Press ENTER.")
	io.read()
end

function bot_EnderChest()
	while turtle.getItemCount(1) ~= 1 do
		term.clear()
		term.setCursorPos(1,1)
		print("Put EnderChest in first slot and press ENTER.")
		io.read()
	end
	firstDropSlot = 2
end

function main()
	if config[7] == 1 then bot_EnderChest() end
	advRefuel()
	bot_refuel(config[4])
	startTime = os.clock()
	if skipShafts then
		skipShaft()
		skipShafts = false
		startShaft = skipShaftsNum + 1
	end
	for x=startShaft, config[2] do
		shaft = x
		if shaft > skipShaftsNum then
			for i=1, config[1] do
				displayUpdate(false)
				bot_checkStatus()
				bot_digTunnel()
				if bot_checkoresUp() or bot_checkoresDown() or bot_checkoresRight() or bot_checkoresLeft() then
					bot_mineOres() 
				end
			end
			if bot_checkoresFornt() then
				bot_mineOres() 
			end
			posDforward = 2
			bot_up()
			if bot_checkoresFornt() or bot_checkoresRight() or bot_checkoresLeft() then
				bot_mineOres() 
			end
			turnDforward(posDforward)
			while posX > 0 do
				displayUpdate(false)
				bot_checkStatus()
				bot_returnTunnel()
				if bot_checkoresUp() or bot_checkoresDown() or bot_checkoresRight() or bot_checkoresLeft() then
					bot_mineOres() 
				end
			end
			bot_down()
			displayUpdate(false)
		end
		if shaft < config[2] then
			nextShaft()
		end
	end
	if config[7] == 1 then bot_dropStuffEnder() end
	bot_returnhome()
	if config[7] == 0 then bot_dropStuffHome() end	
	programEnd()
end

function detectENTER()
        local sEvent, param = os.pullEvent("key")
        if sEvent == "key" then
            if param == 28 then
                print("Enter detected")
            end
        end
end

function RebootDisplayUpdate()
	while true do
		displayUpdate(true)
		print("Press ENTER.")
		sleep(2)
	end
end

if useArgs() then
	if programInspect then
		while true do 
			local has_block, data = turtle.inspect()
			if has_block then
				print(data.name)
			else
				print("No block")
			end
			sleep(0.5)
		end
	end
	if programReset then
		for i=1, #filenames do
			if fs.exists(filenames[i]) then fs.delete(filenames[i]) end
		end
	end
	wlanDisplay()
	configFile()
	if config[6] == 1 and fs.exists(filenames[3]) then
		bot_GPSreboot() --reboot script if GPS is used
	else 
		if DisplaySavefile() then
			DisplayDataTable[8] =  "X-"..DisplayDataTable[8]
			parallel.waitForAny(RebootDisplayUpdate,detectENTER)
			createStartup(false)
			if fs.exists(filenames[4]) then fs.delete(filenames[4]) end
			programTerminate()
		end
		main()
	end
end