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,
- fontSize = 5, -- times 10
- redstoneOut = false
- }
- -- program state
- local isQuitting = 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
- -- functions
- local BoolToRedGreen, FixOffset, IncFontSize, RedstoneOutput
- local Init, Shutdown, ReadSettings, WriteSettings
- local DrawAll, DrawHeader, DrawTable, DrawFooter
- -- loop functions
- local UserInputLoop, RedrawLoop, RadarRefreshLoop
- -- windows
- local mainTerm = term.native
- local radarMap
- 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
- radar.radius = nil
- radar.energyReq = nil
- radar.energyStat = nil
- 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)
- radarMap = window.create(monitor, 1, 1, 1, 1)
- end
- end
- 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")
- RedstoneOutput(settings.redstoneOut and isActive)
- end
- end
- function UserInputLoop()
- while not isQuitting do
- local event, p2, p3, p4, p5 = os.pullEvent("key")
- --debug
- term.setCursorPos(1,10)
- term.clearLine()
- term.write("IL: caught key: " .. tostring(keys.getName(p2)) .. " FS: " .. settings.fontSize)
- --enddebug
- if keys.getName(p2) == "q" then
- isQuitting = true
- elseif keys.getName(p2) == "p" then
- showPositions = not showPositions
- 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) == "r" then
- settings.redstoneOut = not settings.redstoneOut
- 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)
- end
- function DrawRadarStatus(drawDevice)
- drawDevice.setCursorPos(1,2)
- drawDevice.setTextColor(colors.black)
- drawDevice.setBackgroundColor(colors.orange)
- if radar == nil then
- drawDevice.setTextColor(colors.red)
- drawDevice.write("NO RADAR")
- return
- end
- drawDevice.write("RADIUS: ".. radar.radius)
- drawDevice.writeLine("ENERGY REQUIRED: ")
- 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( "[O] Only ores [T] Silk touch [\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] Mine [S] Stop [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)
- radarMap.reposition(1, 1, maxY, maxY)
- DrawMapWindow(radarMap)
- end
- function DrawMapWindow(m)
- local size = math.min(m.getSize())
- if not (size % 2) then
- size = size - 1
- end
- local mid = math.ceil(size/2)
- local tColor = "1"
- local bgColor = "7"
- --debug
- mainTerm.setCursorPos(1,15)
- mainTerm.clearLine()
- mainTerm.write("size:" .. size .. " mid:" .. mid .." " .. os.time("ingame"))
- --enddebug
- --fill
- for py = 2, size-1 do
- m.setCursorPos(1, py)
- for px = 2, size-1 do
- m.blit("\128", tColor, bgColor)
- end
- end
- -- 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)
- Shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement