Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This code is recommended to use together with ComputerCraft Miner OS!
- -- ComputerCraft Miner OS pastebin is YN6pE8gW!
- -- Turtle Start-up
- if not os.loadAPI("data/ExAPI") then
- term.setCursorPos(1,1)
- print("ExAPI is missing!")
- print("You need to have ExAPI in data folder")
- print("ExAPI pastebin is PTK4hM5g")
- print("If you have pastebin enabled type:")
- print("pastebin get PTK4hM5g data/ExAPI")
- error()
- end
- if not term.isColor() then
- print("Program requires an advanced mining turtle.")
- return
- end
- sleep(1)
- Eif not ExAPI.getModemSide() then
- ExAPI.clearScreen()
- ExAPI.writeCenter("Please add a modem to this computer",math.floor(h/2))
- term.setCursorPos(1,1)
- error()
- end
- -- Tables
- startMenu = {
- " ",
- " Start Mining ",
- " Reset Configs ",
- " Reboot ",
- " Shutdown ",
- " "
- }
- contextMenu = {
- " ",
- " Reboot ",
- " Exit ",
- " "
- }
- -- Variables
- w,h = term.getSize()
- osName = tostring(ExAPI.readConfig(1))
- clockTimer = os.startTimer(1)
- src = 0
- computerID = 0
- -- Functions
- -- Turtle Functions
- function readConfig(numb)
- file = fs.open("data/config/cords","r")
- if file then
- curcfg = {}
- line = file.readLine()
- while line do
- table.insert(curcfg,line)
- line = file.readLine()
- end
- file.close()
- else
- defaultConfig()
- readConfig(numb)
- end
- if numb == "all" then
- return curcfg
- else
- return curcfg[numb]
- end
- end
- function checkFuel()
- if turtle.getFuelLevel() < 20 then
- for i=1,16 do
- turtle.select(i)
- turtle.refuel()
- end
- end
- end
- function editConfig(numb,value)
- curcfg = readConfig("all")
- curcfg[numb] = tostring(value)
- file = fs.open("data/config/cords","w")
- for k,v in pairs(curcfg) do
- file.writeLine(curcfg[k])
- end
- file.close()
- end
- function sendInformation()
- info = {}
- fuel = turtle.getFuelLevel()
- table.insert(info,fuel)
- table.insert(info,readConfig(2))
- table.insert(info,readConfig(3))
- info = textutils.serialize(info)
- rednet.send(computerID,info)
- end
- function defaultConfig(times)
- file = fs.open("data/config/cords","w")
- file.writeLine(tostring(1)) -- X
- file.writeLine(tostring(0)) -- Y
- file.writeLine(tostring(0)) -- Back
- file.writeLine("forward") -- Facing
- file.writeLine(tostring(0)) -- Is digging downwards?
- file.writeLine(tostring(15)) -- How much times to loop?
- file.close()
- end
- function goUp(info)
- checkFuel()
- while tonumber(info[2]) > 0 do
- info[2] = info[2] -1
- editConfig(2,info[2])
- turtle.digUp()
- turtle.up()
- editConfig(2,info[2])
- sendInformation()
- end
- end
- function turnLeft(info)
- editConfig(4,"left")
- turtle.turnLeft()
- editConfig(4,"left")
- turtle.dig()
- editConfig(4,"forward")
- turtle.turnRight()
- editConfig(4,"forward")
- end
- function checkSide(info)
- if info[4] == "left" then
- turtle.dig()
- editConfig(4,"forward")
- turtle.turnRight()
- editConfig(4,"forward")
- elseif info[4] == "right" then
- turtle.dig()
- editConfig(4,"forward")
- turtle.turnLeft()
- editConfig(4,"forward")
- end
- end
- function turnRight(info)
- editConfig(4,"right")
- turtle.turnRight()
- editConfig(4,"right")
- turtle.dig()
- editConfig(4,"forward")
- turtle.turnLeft()
- editConfig(4,"forward")
- end
- function goDown(info)
- checkFuel()
- turtle.digDown()
- editConfig(5,1)
- while turtle.down() do
- info[2] = info[2] + 1
- editConfig(2,info[2])
- turtle.digDown()
- sendInformation()
- editConfig(2,info[2])
- turnLeft(info)
- turnRight(info)
- end
- editConfig(5,0)
- end
- function goForward(info)
- checkFuel()
- info[1] = info[1] + 1
- editConfig(1,info[1])
- for i=1,tonumber(info[1]) do
- editConfig(3,info[3])
- sendInformation()
- turtle.dig()
- info[3] = info[3] + 1
- editConfig(3,info[3])
- turtle.forward()
- editConfig(3,info[3])
- end
- end
- function goBack(info)
- checkFuel()
- repeat
- info[3] = info[3] - 1
- editConfig(3,info[3])
- turtle.back()
- editConfig(3,info[3])
- sendInformation()
- until info[3] <= 0
- turtle.dropDown()
- end
- -- Computer Functions
- function startMine(numb)
- for i=1,tonumber(numb) do
- editConfig(6,15-i)
- sendInformation()
- contents = readConfig("all")
- goForward(contents)
- sendInformation()
- contents = readConfig("all")
- goDown(contents)
- sendInformation()
- contents = readConfig("all")
- goUp(contents)
- sendInformation()
- contents = readConfig("all")
- goBack(contents)
- if i >= 15 then
- defaultConfig()
- rednet.send(computerID,"finish")
- return
- end
- end
- end
- -- Safe Check
- content = readConfig("all")
- if tonumber(content[1]) > 1 or tonumber(content[2]) > 0 then
- checkSide(content)
- if tonumber(readConfig(5)) == 1 then
- goDown(content)
- end
- goUp(content)
- goBack(content)
- turnsLeft = tonumber(readConfig(6))
- startMine(turnsLeft)
- end
- function drawDesktop()
- background = paintutils.loadImage("data/backgrounds/.background1")
- paintutils.drawImage(background,1,1)
- ExAPI.clearScreen()
- drawTitle()
- drawBottom()
- end
- function drawExtMenu(contextX,contextY)
- term.setBackgroundColor(128)
- for k,v in pairs(contextMenu) do
- term.setCursorPos(contextX,contextY - 1 + k)
- term.write(tostring(v))
- end
- end
- function drawTitle()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.blue)
- term.clearLine()
- ExAPI.writeCenter(osName,1)
- end
- function drawBottom()
- term.setBackgroundColor(colors.green)
- for i=1,2 do
- term.setCursorPos(1,h-2+i)
- term.clearLine()
- end
- term.setBackgroundColor(colors.blue)
- ExAPI.writeLeft(" [Start] ",0,h-1)
- ExAPI.writeLeft(" ",0,h)
- end
- function drawMenu()
- term.setBackgroundColor(128)
- for k,v in pairs(startMenu) do
- ExAPI.writeLeft(tostring(v),0,h-#startMenu-2+k)
- end
- end
- function drawExtMenu(contextX,contextY)
- term.setBackgroundColor(128)
- for k,v in pairs(contextMenu) do
- term.setCursorPos(contextX,contextY - 1 + k)
- term.write(tostring(v))
- end
- end
- function displayTime()
- term.setBackgroundColor(colors.green)
- time = textutils.formatTime(os.time(),true)
- ExAPI.writeRight(time,2,h-1)
- end
- -- Main Loop
- drawDesktop()
- displayTime()
- while true do
- event, button, X, Y = os.pullEvent()
- if event == "rednet_message" and X ~= "ExMR" then
- computerID = button
- sendInformation()
- if X == "startMine" then
- sendInformation()
- startMine(15)
- end
- elseif event == "mouse_click" then
- if src == 0 then
- if X >= 1 and X <= 15 and Y >= h-1 and button == 1 then
- src = 1
- drawMenu()
- elseif X >= 1 and Y >= 2 and button == 2 then
- src = 2
- if X >= w - 11 then
- contextX = w - 11
- end
- if X >= h - 7 then
- contextY = h - 6
- end
- if X <= w - 11 then
- contextX = X
- end
- if Y <= h - 4 then
- contextY = Y
- end
- drawExtMenu(contextX,contextY)
- end
- elseif src == 1 then
- if X >= 1 and X <= 15 and Y == h-3 and button == 1 then
- src = 0
- os.shutdown()
- elseif X >= 1 and X <= 15 and Y == h-4 and button == 1 then
- src = 0
- os.reboot()
- elseif X >= 1 and X <= 15 and Y == h-5 and button == 1 then
- src = 0
- defaultConfig()
- drawDesktop()
- displayTime()
- elseif X >= 1 and X <= 15 and Y == h-6 and button == 1 then
- src = 0
- drawDesktop()
- displayTime()
- sendInformation()
- startMine(15)
- else
- src = 0
- drawDesktop()
- displayTime()
- end
- elseif src == 2 then
- if X >= contextX and X <= contextX + 10 and Y == contextY + 1 and button == 1 then
- src = 0
- os.reboot()
- elseif X >= contextX and X <= contextX + 10 and Y == contextY + 2 and button == 1 then
- src = 0
- term.setBackgroundColor(colors.black)
- ExAPI.clearScreen()
- error()
- else
- src = 0
- drawDesktop()
- displayTime()
- end
- end
- elseif event == "timer" then
- if button == clockTimer then
- rednet.broadcast("ExMR")
- displayTime()
- clockTimer = os.startTimer(1)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement