Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- settings
- local defaultSettings = {
- -- minerRefreshSeconds = 0.2,
- -- inactiveMinerRefreshSeconds = 5,
- -- silkTouch = false,
- -- onlyOres = true,
- -- laserOffset = 8,
- globalCoordinates = true,
- fontSize = 5, -- times 10
- redstoneOut = false
- }
- -- program state
- local isQuitting = false
- local isScanning = false
- -- local startMining = false
- -- local stopMining = false
- -- local isActive = false
- -- local laserCnt = 0
- -- local laserStartingH = 0
- -- local laserSettingsChanged = true
- local energyUnits = nil
- local showPositions = false
- local radar = {}
- local settings = {}
- local monitor = nil
- local globalToLocalX, globalToLocalY
- -- functions
- local BoolToRedGreen, IncFontSize, RedstoneOutput
- local Init, Shutdown, ReadSettings, WriteSettings
- local DrawAll, DrawHeader, DrawTable, DrawFooter
- -- loop functions
- local UserInputLoop, RedrawLoop, RadarRefreshLoop
- -- windows
- local mainTerm = term.native()
- local radarMapWindow, radarStatusWindow, radarResultsWindow
- function BoolToRedGreen(value)
- if value then
- return colors.green
- else
- return colors.red
- end
- end
- function IncFontSize(currentSize)
- if currentSize >= 50 or currentSize < 5 then
- return 5
- else
- return currentSize + 5
- end
- end
- function RedstoneOutput(emit)
- for _, side in pairs(redstone.getSides()) do
- rs.setOutput(side, emit)
- end
- end
- function ReadSettings()
- local file = fs.open("radarcontrol.cfg","r")
- if file ~= nil then
- settings = textutils.unserialize(file.readAll())
- end
- for k,v in pairs (defaultSettings) do
- if settings[k] == nil then
- settings[k] = defaultSettings[k]
- end
- end
- for k,v in pairs (settings) do
- print("Settings: " .. tostring(k) .. " : " .. tostring(v))
- end
- end
- function WriteSettings()
- local file = fs.open("radarcontrol.cfg","w")
- file.write(textutils.serialize(settings))
- file.close()
- end
- function Init()
- if not term.isColor() then
- print("Advanced computer required. Go find some gold.")
- end
- ReadSettings()
- WriteSettings()
- local sides = peripheral.getNames()
- for _, side in pairs(sides) do
- if peripheral.getType(side) == "warpdriveRadar" then
- radar.peripheral = peripheral.wrap(side)
- print("Wrapped warpdriveRadar on " .. side .. " side")
- if energyUnits == nil then
- energyUnits = radar.peripheral.energyDisplayUnits()
- end
- local ok, galaxy, gx, gy, gz, planet = radar.peripheral.getGlobalPosition()
- if ok then
- radar.x = gx
- radar.y = gy
- radar.z = gz
- else
- print("Radar position failure")
- return
- end
- local lx, ly, lz = radar.peripheral.getLocalPosition()
- globalToLocalX = lx - gx
- globalToLocalY = ly - gy
- elseif monitor == nil and peripheral.getType(side) == "monitor" then
- print("Wrapping monitor on " .. side .. " side")
- monitor = peripheral.wrap(side)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- monitor.setTextScale(settings.fontSize / 10.0)
- monitor.setPaletteColor(colors.lightGray, 0.4, 0.4, 0.4)
- radarMapWindow = window.create(monitor, 1, 1, 1, 1)
- end
- end
- radarStatusWindow = window.create(mainTerm, 1, 2, 25, 10)
- radarResultsWindow = window.create(mainTerm, 26, 2, 25, 10)
- sleep(1)
- term.clear()
- term.setPaletteColor(colors.lightGray, 0.4, 0.4, 0.4)
- os.queueEvent("redraw")
- end
- function Shutdown()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setPaletteColor(colors.lightGray, term.nativePaletteColor(colors.lightGray))
- term.setCursorPos(1,1)
- RedstoneOutput(false)
- end
- function RadarRefreshLoop()
- while not isQuitting do
- local event = os.pullEvent("updateRadarEvent")
- if radar == nil then return end
- isScanning = true
- radar.peripheral.start()
- sleep(radar.peripheral.getScanDuration() + 1)
- isScanning = false
- radar.results = {}
- for i=1, radar.peripheral.getResultsCount() do
- local success, _, name, cx, cy, cz = radar.peripheral.getResult(i)
- if success then
- table.insert(radar.results, {name = tostring(name), gx = tonumber(x), gy = tonumber(y), gz = tonumber(z)})
- end
- end
- os.queueEvent("redraw")
- end
- end
- function UserInputLoop()
- while not isQuitting do
- local event, p2, p3, p4, p5 = os.pullEvent()
- if event == "key" then
- if keys.getName(p2) == "q" then
- isQuitting = true
- elseif keys.getName(p2) == "p" then
- showPositions = not showPositions
- elseif keys.getName(p2) == "s" then
- os.queueEvent("updateRadarEvent")
- elseif keys.getName(p2) == "f" then
- if monitor ~= nil then
- settings.fontSize = IncFontSize(settings.fontSize)
- monitor.setTextScale(settings.fontSize / 10.0)
- end
- elseif keys.getName(p2) == "down" then
- radar.peripheral.radius(radar.peripheral.radius() - 1000)
- laserSettingsChanged = true
- elseif keys.getName(p2) == "up" then
- radar.peripheral.radius(radar.peripheral.radius() + 1000)
- elseif keys.getName(p2) == "right" then
- radar.peripheral.radius(radar.peripheral.radius() + 100)
- elseif keys.getName(p2) == "left" then
- radar.peripheral.radius(radar.peripheral.radius() - 100)
- elseif keys.getName(p2) == "r" then
- settings.redstoneOut = not settings.redstoneOut
- end
- elseif event == "mouse_scroll" then
- if p2 == 1 then
- radarResultsWindow.properties.startPosition = radarResultsWindow.properties.startPosition + 1
- elseif p2 == -1 then
- radarResultsWindow.properties.startPosition = radarResultsWindow.properties.startPosition - 1
- end
- end
- --debug
- term.setCursorPos(1,14)
- term.clearLine()
- term.write("IL: " .. " FS: " .. settings.fontSize .." " .. os.time("ingame"))
- --enddebug
- WriteSettings()
- os.queueEvent("redraw")
- end
- end
- function RedrawLoop()
- while not isQuitting do
- local event = os.pullEvent("redraw")
- DrawAll()
- end
- end
- function DrawAll()
- DrawHeader(term)
- -- DrawHeader(monitor)
- DrawFooter(term)
- DrawMonitor(monitor)
- DrawRadarStatus(radarStatusWindow)
- DrawRadarResults(radarResultsWindow)
- end
- function DrawRadarStatus(drawDevice)
- drawDevice.setTextColor(colors.black)
- drawDevice.setBackgroundColor(colors.orange)
- drawDevice.clear()
- drawDevice.setCursorPos(1,1)
- if radar == nil then
- drawDevice.setTextColor(colors.red)
- drawDevice.write("NO RADAR")
- return
- end
- local eAvail, eFull, eUnits = radar.peripheral.getEnergyStatus()
- local ok, eReq = radar.peripheral.getEnergyRequired()
- local radius = radar.peripheral.radius()
- local duration = radar.peripheral.getScanDuration()
- if (eAvail == nil or eFull == nil or eReq == nil or duration == nil) then return end
- drawDevice.write("Radius: ".. radius .. " m")
- drawDevice.setCursorPos(1,2)
- drawDevice.write(string.format("Needed: %dM EU %d%%", eReq / 1000000, eAvail / eReq * 100))
- drawDevice.setCursorPos(1,3)
- drawDevice.write(string.format("Total: %d%%", eAvail / eFull * 100))
- drawDevice.setCursorPos(1,4)
- drawDevice.write(string.format("Scan duration: %ds", duration))
- drawDevice.setCursorPos(1,5)
- drawDevice.write(string.format("Results: %d", radar.peripheral.getResultsCount()))
- end
- function DrawRadarResults(drawDevice)
- drawDevice.setTextColor(colors.black)
- drawDevice.setBackgroundColor(colors.yellow)
- drawDevice.clear()
- drawDevice.setCursorPos(1,1)
- if radar == nil or radar.results == nil then
- drawDevice.write("No radar or results")
- return
- end
- local maxX, maxY = drawDevice.getSize()
- local resultsCount = table.getn(radar.results)
- --window properties
- if radarResultsWindow.properties == nil then radarResultsWindow.properties = {startPosition = 1} end
- if radarResultsWindow.properties.startPosition > (resultsCount - maxY) then radarResultsWindow.properties.startPosition = math.max(resultsCount - maxY, 1) end
- --table
- for py = 1, maxY do
- position = py + radarResultsWindow.properties.startPosition
- if position <= resultsCount then
- drawDevice.setCursorPos(1, py)
- drawDevice.write(string.format("%d %s", py, radar.results[position].name))
- end
- end
- --scrollbar
- if radarResultsWindow.properties.startPosition > 1 then
- drawDevice.setCursorPos(maxX, 1)
- drawDevice.write("\30")
- end
- if (resultsCount - radarResultsWindow.properties.startPosition - maxY) > 0 then
- drawDevice.setCursorPos(maxX, maxY)
- drawDevice.write("\31")
- end
- end
- function DrawHeader(drawDevice)
- if drawDevice == nil then
- return
- end
- local maxX, maxY = drawDevice.getSize()
- local str = "RadarControl v1 by DrPepper"
- drawDevice.setCursorPos(math.ceil((maxX - string.len(str)) / 2 + 1), 1)
- drawDevice.setTextColor(colors.orange)
- drawDevice.setBackgroundColor(colors.black)
- drawDevice.clearLine()
- drawDevice.write(str)
- if isQuitting then
- drawDevice.setCursorPos(1,1)
- drawDevice.setTextColor(colors.white)
- drawDevice.setBackgroundColor(colors.black)
- drawDevice.clearLine()
- drawDevice.write("Quitting...")
- end
- end
- --###################################################
- --# Status Energy Hei Min/Tot
- --99 o Status message 123456 EU 999 123/456
- function DrawTable(drawDevice)
- if drawDevice == nil then
- return
- end
- local maxX, maxY = drawDevice.getSize()
- drawDevice.setCursorPos(1, 3)
- drawDevice.setTextColor(colors.black)
- drawDevice.setBackgroundColor(colors.orange)
- drawDevice.clearLine()
- drawDevice.write("# Status")
- local xAlign = maxX - 21
- if xAlign < 5 then xAlign = 5 end
- drawDevice.setCursorPos(xAlign, 3)
- drawDevice.write(string.format(" %-9.9s %-3.3s %-3.3s/%-3.3s", "Energy", "Height", "Mined", "Total"))
- drawDevice.setCursorPos(1, 4)
- for k,v in pairs (lasers) do
- local cx, cy = drawDevice.getCursorPos()
- if cy % 2 == 1 then
- drawDevice.setBackgroundColor(colors.gray)
- else
- drawDevice.setBackgroundColor(colors.lightGray)
- end
- drawDevice.setTextColor(colors.white)
- drawDevice.clearLine()
- drawDevice.write(string.format("%2d", tonumber(k)))
- drawDevice.setTextColor(BoolToRedGreen(v.isActive))
- drawDevice.write(" \7 ")
- drawDevice.setTextColor(colors.white)
- local status = tostring(v.status)
- if showPositions then
- status = tostring(v.x) .. "; " .. tostring(v.y) .. "; " .. tostring(v.z)
- end
- drawDevice.write(status)
- drawDevice.setCursorPos(xAlign, cy)
- drawDevice.write(string.format(" %-6s %-2.2s %3s %3.3s/%-3.3s",
- tostring(v.energy),
- energyUnits,
- tostring(v.currentLayer),
- tostring(v.mined),
- tostring(v.total)))
- drawDevice.setCursorPos(1, cy + 1)
- end
- end
- function DrawFooter(drawDevice)
- local maxX, maxY = drawDevice.getSize()
- drawDevice.setTextColor(colors.white)
- drawDevice.setBackgroundColor(colors.gray)
- drawDevice.setCursorPos(1, maxY-2)
- drawDevice.clearLine()
- drawDevice.blit( "[P] Toggle position [F] Font size [R] Redstone",
- "01000000000000000000000100000000000000010000000000",
- "77777777777777777777777777777777777777777777777777" )
- drawDevice.setBackgroundColor(colors.gray)
- drawDevice.setCursorPos(1, maxY-1)
- drawDevice.clearLine()
- drawDevice.blit( " [ ] [\24\25\26\27] Offset:",
- "01000000000000000100000000000000001111000000000",
- "77777777777777777777777777777777777777777777777" )
- drawDevice.setBackgroundColor(colors.black)
- drawDevice.write(string.format(" %3s", settings.laserOffset))
- drawDevice.setBackgroundColor(colors.gray)
- drawDevice.setCursorPos(1, maxY)
- drawDevice.clearLine()
- drawDevice.blit( "[M] [S] Scan [Q] Exit",
- "010000000000100000000001000000",
- "777777777777777777777777777777" )
- end
- function DrawMonitor(m)
- if m == nil then
- return
- end
- local maxX, maxY = m.getSize()
- --assuming landscape orientation
- -- DrawMap(m, 1, 1, maxY)
- radarMapWindow.reposition(1, 1, maxY, maxY)
- DrawMapWindow(radarMapWindow)
- end
- function DrawMapWindow(m)
- local size = math.min(m.getSize())
- if size % 2 == 0 then
- size = size - 1
- end
- local mid = math.ceil(size/2)
- local tColor = "1"
- local bgColor = "7"
- m.setBackgroundColor(colors.gray)
- m.clear()
- --debug
- mainTerm.setCursorPos(1,15)
- mainTerm.clearLine()
- mainTerm.write("size:" .. size .. " mid:" .. mid .." " .. os.time("ingame"))
- --enddebug
- -- horizontal lines
- for px = 2, size-1 do
- m.setCursorPos(px, 1)
- m.blit("\131", tColor, bgColor)
- m.setCursorPos(px, size)
- m.blit("\143", bgColor, tColor)
- m.setCursorPos(px, mid)
- m.blit("\140", tColor, bgColor)
- end
- -- vertical lines
- for py = 2, size-1 do
- m.setCursorPos(1, py)
- m.blit("\149", tColor, bgColor)
- m.setCursorPos(size, py)
- m.blit("\149", bgColor, tColor)
- m.setCursorPos(mid, py)
- m.blit("\149", tColor, bgColor)
- end
- --corners
- m.setCursorPos(1, 1)
- m.blit("\151", tColor, bgColor)
- m.setCursorPos(size, 1)
- m.blit("\148", bgColor, tColor)
- m.setCursorPos(1, size)
- m.blit("\138", bgColor, tColor)
- m.setCursorPos(size, size)
- m.blit("\133", bgColor, tColor)
- m.setCursorPos(mid, 1)
- m.blit("\151", tColor, bgColor)
- m.setCursorPos(1, mid)
- m.blit("\157", tColor, bgColor)
- m.setCursorPos(mid, mid)
- m.blit("\157", tColor, bgColor)
- m.setCursorPos(size, mid)
- m.blit("\145", bgColor, tColor)
- m.setCursorPos(mid, size)
- m.blit("\138", bgColor, tColor)
- end
- -- drawDevice, top left x, top lefty, size (width and height) in symbols
- function DrawMap(m, x, y, size)
- if not (size % 2) then
- size = size - 1
- end
- if size < 3 then return end
- local midX = x + math.floor(size/2)
- local midY = y + math.floor(size/2)
- local tColor = "0"
- local bgColor = "7"
- --fill
- for py = y+1, y+size-2 do
- m.setCursorPos(x+1, py)
- for px = x+1, x+size-2 do
- m.blit("\128", tColor, bgColor)
- end
- end
- -- horizontal lines
- for px = x+1, x+size-1 do
- m.setCursorPos(px, y)
- m.blit("\131", tColor, bgColor)
- m.setCursorPos(px, y + size - 1)
- m.blit("\143", bgColor, tColor)
- m.setCursorPos(px, midY)
- m.blit("\140", tColor, bgColor)
- end
- -- vertical lines
- for py = y+1, y+size-1 do
- m.setCursorPos(x, py)
- m.blit("\149", tColor, bgColor)
- m.setCursorPos(x + size - 1, py)
- m.blit("\149", bgColor, tColor)
- m.setCursorPos(midX, py)
- m.blit("\149", tColor, bgColor)
- end
- --corners
- m.setCursorPos(x, y)
- m.blit("\151", tColor, bgColor)
- m.setCursorPos(x+size-1, y)
- m.blit("\148", bgColor, tColor)
- m.setCursorPos(x, y+size-1)
- m.blit("\138", bgColor, tColor)
- m.setCursorPos(x+size-1, y+size-1)
- m.blit("\133", bgColor, tColor)
- m.setCursorPos(midX, y)
- m.blit("\151", tColor, bgColor)
- m.setCursorPos(x, midY)
- m.blit("\157", tColor, bgColor)
- m.setCursorPos(midX, midY)
- m.blit("\157", tColor, bgColor)
- m.setCursorPos(x+size-1, midY)
- m.blit("\145", bgColor, tColor)
- m.setCursorPos(midX, y+size-1)
- m.blit("\138", bgColor, tColor)
- end
- Init()
- parallel.waitForAny(RedrawLoop, UserInputLoop, RadarRefreshLoop)
- Shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement