Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables
- turtle = {}
- dist = {}
- timer1 = os.startTimer(1)
- i= 0
- turtles = 0
- function getModemSide()
- local sides = {"left","right","top","front","back","bottom"}
- for i=1,6 do
- if peripheral.isPresent(sides[i]) then
- if peripheral.getType(sides[i]) == "modem" then
- rednet.open(sides[i])
- return true
- end
- end
- end
- return false
- end
- function clearScreen()
- term.clear()
- term.setCursorPos(1,1)
- end
- function drawInputBox(name)
- term.setBackgroundColor(colors.blue)
- for i=1,7 do
- writeCenter(" ",math.floor(h/2-4+i))
- end
- term.setTextColor(colors.red)
- writeCenter(name,h/2-4+1)
- term.setBackgroundColor(128)
- writeCenter(" ",h/2-4+4)
- term.setCursorPos(w-40,h/2-4+4)
- term.setTextColor(1)
- input = read()
- return input
- end
- function writeCenter(text,y)
- local w,h = term.getSize()
- if y == nil or y == 0 then
- y = h + 1
- end
- term.setCursorPos(math.floor(w-#text)/2,y)
- term.write(text)
- end
- function writeLeft(text,spaces,y)
- term.setCursorPos(1+spaces,y)
- term.write(text)
- end
- function writeRight(text,spaces,y)
- local w,h = term.getSize()
- term.setCursorPos(w-#text-spaces,y)
- term.write(text)
- end
- function makeConfig()
- fs.makeDir("data/config")
- file = fs.open("data/config/config","w")
- file.writeLine("Exploder's OS")
- file.writeLine("1")
- file.writeLine("1")
- file.writeLine("0")
- file.close()
- end
- function makeTurtleConfig()
- fs.makeDir("data/config")
- file = fs.open("data/config/turtleID","w")
- file.close()
- end
- function editConfig(numb,value)
- curcfg = readConfig("all")
- curcfg[numb] = tostring(value)
- file = fs.open("data/config/config","w")
- for k,v in pairs(curcfg) do
- file.writeLine(curcfg[k])
- end
- file.close()
- end
- function editTurtleConfig(numb,value)
- curcfg = readTurtleConfig("all")
- file = fs.open("data/config/turtleID","w")
- for k,v in pairs(turtle) do
- file.writeLine(tostring(turtle[k]))
- end
- file.close()
- end
- function readTurtleConfig(numb)
- file = fs.open("data/config/turtleID","r")
- if file then
- curcfg = {}
- line = file.readLine()
- while line do
- table.insert(curcfg,line)
- line = file.readLine()
- end
- file.close()
- else
- makeTurtleConfig()
- readTurtleConfig(numb)
- end
- if numb == "all" then
- return curcfg
- else
- return curcfg[numb]
- end
- end
- function readConfig(numb)
- file = fs.open("data/config/config","r")
- if file then
- curcfg = {}
- line = file.readLine()
- while line do
- table.insert(curcfg,line)
- line = file.readLine()
- end
- file.close()
- else
- makeConfig()
- readConfig(numb)
- end
- if numb == "all" then
- return curcfg
- else
- return curcfg[numb]
- end
- end
- function randomBackground(numb)
- bin = numb
- if tonumber(bin) > 2 then
- bin = math.random(1,tonumber(bin)-1)
- else
- bin = 1
- end
- return bin
- end
- function showInfo(numb)
- local w,h = term.getSize()
- local turtle = readTurtleConfig(numb)
- term.setBackgroundColor(colors.cyan)
- clearScreen()
- term.setBackgroundColor(colors.orange)
- for i=1, h-4 do
- term.setCursorPos(2,2+i)
- write(" ")
- end
- for i=1,49 do
- term.setCursorPos(1+i,3)
- term.write("-")
- term.setCursorPos(1+i,h-2)
- term.write("-")
- term.setCursorPos(1+i,5)
- term.write("-")
- term.setCursorPos(1+i,7)
- term.write("-")
- term.setCursorPos(1+i,9)
- term.write("-")
- term.setCursorPos(1+i,h-6)
- term.write("-")
- term.setCursorPos(1+i,h-4)
- term.write("-")
- end
- for i=1,h-4 do
- term.setCursorPos(2,2+i)
- term.write(":")
- term.setCursorPos(50,2+i)
- term.write(":")
- end
- writeCenter("[Back]",h-3)
- writeCenter("Turtle ID: "..turtle.." control panel",4)
- term.setTextColor(colors.blue)
- writeLeft("[Start Turtle]",6,h-5)
- term.setTextColor(colors.red)
- writeRight("[Stop Turtle]",6,h-5)
- term.setTextColor(colors.white)
- -- Getting information from the turtle.
- while true do
- rednet.send(tonumber(turtle),"info")
- if tonumber(readConfig(4)) == 1 then
- break
- end
- event, p1,p2,p3,p4 = os.pullEvent()
- if event == "mouse_click" then
- if p2 >= 22 and p2 <= 27 and p3 == 16 and p1 == 1 then
- turtleControl()
- end
- elseif event == "rednet_message" and p1 == tonumber(turtle) then
- info = textutils.unserialize(p2)
- writeLeft("Fuel Level: ",4,6)
- writeLeft("Distance Forward: ",4,8)
- writeRight("Depth: ",4,6)
- writeRight("Distance: ",4,8)
- writeCenter("Messages",h-9)
- if type(info) == "table" then
- if tonumber(info[1]) >= 1000 then term.setTextColor(colors.green)
- elseif tonumber(info[1]) < 1000 then term.setTextColor(colors.red)
- end
- writeLeft(tostring(info[1]),16,6)
- writeLeft(tostring(info[3]),22,8)
- if tonumber(info[1]) < 1185 then
- term.setTextColor(colors.red)
- writeCenter("Warning, fuel is too low for one full run",h-8)
- end
- if tonumber(info[2]) >= 50 then term.setTextColor(colors.red)
- elseif tonumber(info[2]) < 50 then term.setTextColor(colors.green)
- end
- writeRight(tostring(info[2]),3,6)
- writeRight(tostring(math.floor(p3)),3,8)
- term.setTextColor(colors.white)
- if tonumber(info[3]) >= 1 then
- editConfig(4,1)
- end
- break
- end
- end
- end
- -- Main Loop
- while true do
- rednet.send(tonumber(turtle),"info")
- event,button, X, Y = os.pullEvent()
- if event == "mouse_click" then
- if X >= 22 and X <= 27 and Y == 16 and button == 1 then
- turtleControl()
- elseif X >= 7 and X <= 20 and Y == 14 and button == 1 then
- rednet.send(tonumber(turtle),"startMine")
- editConfig(4,1)
- elseif X >= 32 and X <= 44 and Y == 14 and button == 1 then
- rednet.send(tonumber(turtle),"stopMine")
- editConfig(4,0)
- end
- elseif event == "rednet_message" and tonumber(readConfig(4)) == 1 then
- if button == tonumber(turtle) then
- info = textutils.unserialize(X)
- writeLeft("Fuel Level: ",4,6)
- writeLeft("Distance Forward: ",4,8)
- writeRight("Depth: ",4,6)
- writeRight("Distance: ",4,8)
- writeCenter("Messages",h-9)
- if type(info) == "table" then
- if tonumber(info[1]) >= 1000 then term.setTextColor(colors.green)
- elseif tonumber(info[1]) < 1000 then term.setTextColor(colors.red)
- end
- writeLeft(tostring(info[1]),16,6)
- writeLeft(tostring(info[3]),22,8)
- if tonumber(info[1]) < 1185 then
- term.setTextColor(colors.red)
- writeCenter("Warning, fuel is too low for one full run",h-8)
- end
- if tonumber(info[2]) >= 50 then term.setTextColor(colors.red)
- elseif tonumber(info[2]) < 50 then term.setTextColor(colors.green)
- end
- writeRight(tostring(info[2]),3,6)
- writeRight(tostring(math.floor(Y)),3,8)
- term.setTextColor(colors.white)
- end
- elseif button == tonumber(turtle) and X == "finish" then
- editConfig(4,0)
- end
- end
- end
- end
- function turtleScan()
- local w,h = term.getSize()
- term.setBackgroundColor(colors.cyan)
- clearScreen()
- term.setBackgroundColor(colors.orange)
- for i=1, h-4 do
- term.setCursorPos(2,2+i)
- write(" ")
- end
- for i=1,49 do
- term.setCursorPos(1+i,3)
- term.write("-")
- term.setCursorPos(1+i,h-2)
- term.write("-")
- term.setCursorPos(1+i,5)
- term.write("-")
- term.setCursorPos(1+i,h-4)
- term.write("-")
- term.setCursorPos(1+i,7)
- term.write("-")
- end
- for i=1,h-4 do
- term.setCursorPos(2,2+i)
- term.write(":")
- term.setCursorPos(50,2+i)
- term.write(":")
- end
- writeCenter("Turtle scanner",4)
- writeLeft("[Save & Back]",5,h-3)
- writeRight("[Back]",5,h-3)
- writeCenter("Turtles Found: "..turtles,6)
- if turtles >= 1 then
- for k,v in pairs(turtle) do
- writeCenter("TurtleID: "..turtle[k].." Distance: "..dist[k],7+k)
- end
- end
- while true do
- local event, button, X, Y = os.pullEvent()
- if event == "mouse_click" then
- if X >= 6 and X <= 18 and Y == 16 then
- for k,v in pairs(turtle) do
- editTurtleConfig(k,v)
- end
- os.reboot()
- elseif X >= w-11 and X <= w-6 and Y == 16 then
- os.reboot()
- end
- elseif event == "rednet_message" then
- if X == "ExMR" then
- if turtles >= 1 then
- for k,v in pairs(turtle) do
- if turtle[k] == button then
- turtleScan()
- end
- end
- end
- turtles = turtles + 1
- table.insert(turtle,button)
- table.insert(dist,math.floor(Y))
- turtleScan()
- end
- end
- end
- end
- function turtleControl()
- local w,h = term.getSize()
- term.setBackgroundColor(colors.cyan)
- clearScreen()
- term.setBackgroundColor(colors.orange)
- for i=1, h-4 do
- term.setCursorPos(2,2+i)
- write(" ")
- end
- for i=1,49 do
- term.setCursorPos(1+i,3)
- term.write("-")
- term.setCursorPos(1+i,h-2)
- term.write("-")
- term.setCursorPos(1+i,5)
- term.write("-")
- end
- for i=1,h-4 do
- term.setCursorPos(2,2+i)
- term.write(":")
- term.setCursorPos(50,2+i)
- term.write(":")
- end
- curcfg = readTurtleConfig("all")
- writeCenter("Turtle Control Panel",4)
- writeCenter("[Back]",h-3)
- for i=1,#curcfg do
- writeCenter("[Turtle number: "..i.." ID: "..curcfg[i].."]",5+i)
- end
- if #curcfg < 1 then
- writeCenter("[No turtles are stored.]",10)
- end
- while true do
- event, button, X, Y = os.pullEvent()
- if event == "mouse_click" then
- if X >= 22 and X <= 27 and Y == 16 and button == 1 then
- os.reboot()
- elseif X >= 13 and X <= 36 and Y == 6 and button == 1 and #curcfg >= 1 then
- showInfo(1)
- elseif X >= 13 and X <= 36 and Y == 7 and button == 1 and #curcfg >= 2 then
- showInfo(2)
- elseif X >= 13 and X <= 36 and Y == 8 and button == 1 and #curcfg >= 3 then
- showInfo(3)
- elseif X >= 13 and X <= 36 and Y == 9 and button == 1 and #curcfg >= 4 then
- showInfo(4)
- elseif X >= 13 and X <= 36 and Y == 10 and button == 1 and #curcfg >= 5 then
- showInfo(5)
- elseif X >= 13 and X <= 36 and Y == 11 and button == 1 and #curcfg >= 6 then
- showInfo(6)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement