Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not term.isColor() then
- print("Advanced computer required")
- error()
- end
- print("loading...")
- monitor_textScale = 0.5
- Style = {
- CDefault = colors.black,
- BGDefault = colors.lightGray,
- CTitle = colors.orange,
- BGTitle = colors.black,
- CFooter = colors.white,
- BGFooter = colors.blue,
- CWarning = colors.white,
- BGWarning = colors.red,
- CSuccess = colors.white,
- BGSuccess = colors.lime,
- CDisabled = colors.gray,
- BGDisabled = colors.blue,
- CRadarmap = colors.gray,
- BGRadarmap = colors.green,
- CRadarborder = colors.white,
- BGRadarborder = colors.black,
- CRadarself = colors.white,
- BGRadarself = colors.lime,
- TextRadarself = "R",
- CRadarother = colors.black,
- BGRadarother = colors.red,
- TextRadarother = "#"
- }
- ----------- Monitor support
- function SetMonitorColorFrontBack(frontColor, backgroundColor)
- term.setBackgroundColor(backgroundColor)
- term.setTextColor(frontColor)
- if monitors ~= nil then
- for key,monitor in pairs(monitors) do
- monitor.setTextColor(frontColor)
- monitor.setBackgroundColor(backgroundColor)
- end
- end
- end
- function Write(text)
- term.write(text)
- if monitors ~= nil then
- for key,monitor in pairs(monitors) do
- if key ~= data.radar_monitorIndex then
- monitor.write(text)
- end
- end
- end
- end
- local function getCursorPos()
- local x, y = term.getCursorPos()
- return x, y
- end
- function SetCursorPos(x, y)
- term.setCursorPos(x, y)
- if monitors ~= nil then
- for key,monitor in pairs(monitors) do
- if key ~= data.radar_monitorIndex then
- monitor.setCursorPos(x, y)
- end
- end
- end
- end
- local function getResolution()
- local sizeX, sizeY = term.getSize()
- return sizeX, sizeY
- end
- function SetColorDefault()
- SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
- end
- function SetColorTitle()
- SetMonitorColorFrontBack(Style.CTitle, Style.BGTitle)
- end
- function SetColorFooter()
- SetMonitorColorFrontBack(Style.CFooter, Style.BGFooter)
- end
- function SetColorWarning()
- SetMonitorColorFrontBack(Style.CWarning, Style.BGWarning)
- end
- function SetColorSuccess()
- SetMonitorColorFrontBack(Style.CSuccess, Style.BGSuccess)
- end
- function SetColorDisabled()
- SetMonitorColorFrontBack(Style.CDisabled, Style.BGDisabled)
- end
- function SetColorNormal()
- SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
- end
- function SetColorRadarmap()
- SetMonitorColorFrontBack(Style.CRadarmap, Style.BGRadarmap)
- end
- function SetColorRadarborder()
- SetMonitorColorFrontBack(Style.CRadarborder, Style.BGRadarborder)
- end
- local function clear(colorFront, colorBack)
- clearWarningTick = -1
- if CDefault == nil or BGDefault == nil then
- SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
- else
- SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
- end
- term.clear()
- if monitors ~= nil then
- for key, monitor in pairs(monitors) do
- if key ~= data.radar_monitorIndex then
- monitor.clear()
- end
- end
- end
- SetCursorPos(1, 1)
- end
- local function clearLine()
- term.clearLine()
- if monitors ~= nil then
- for key, monitor in pairs(monitors) do
- if key ~= data.radar_monitorIndex then
- monitor.clearLine()
- end
- end
- end
- local x, y = getCursorPos()
- SetCursorPos(1, y)
- end
- function WriteLn(text)
- Write(text)
- local x, y = term.getCursorPos()
- local width, height = term.getSize()
- if y > height - 1 then
- y = 1
- end
- SetCursorPos(1, y + 1)
- end
- function WriteCentered(y, text)
- local unused
- if text == nil then
- text = y
- unused, y = getCursorPos()
- end
- SetCursorPos((51 - text:len()) / 2, y)
- term.write(text)
- if monitors ~= nil then
- for key,monitor in pairs(monitors) do
- if key ~= data.radar_monitorIndex then
- local sizeX, sizeY = monitor.getSize()
- if sizeX ~= nil then
- monitor.setCursorPos((sizeX - text:len()) / 2, y)
- monitor.write(text)
- end
- end
- end
- end
- local xt, yt = term.getCursorPos()
- SetCursorPos(1, y + 1)
- end
- local function page_begin(text)
- clear()
- SetColorTitle()
- SetCursorPos(1, 1)
- SetColorTitle()
- clearLine()
- WriteCentered(1, text)
- SetCursorPos(1, 2)
- SetColorNormal()
- end
- function ShowTitle(text)
- clear()
- SetCursorPos(0, 1)
- SetColorTitle()
- clearLine()
- WriteCentered(1, text)
- SetCursorPos(1, 2)
- SetColorDefault()
- end
- function ShowMenu(text)
- Write(text)
- local sizeX, sizeY = term.getSize()
- local xt, yt = term.getCursorPos()
- for i = xt, sizeX do
- Write(" ")
- end
- SetCursorPos(1, yt + 1)
- end
- local clearWarningTick = -1
- function ShowWarning(text)
- local sizeX, sizeY = term.getSize()
- SetCursorPos(1, sizeY)
- clearLine()
- SetColorWarning()
- SetCursorPos((sizeX - text:len() - 2) / 2, sizeY)
- Write(" " .. text .. " ")
- SetColorDefault()
- clearWarningTick = 5
- -- clearLine()
- end
- function ClearWarning()
- if clearWarningTick > 0 then
- clearWarningTick = clearWarningTick - 1
- elseif clearWarningTick == 0 then
- SetColorDefault()
- local sizeX, sizeY = term.getSize()
- SetCursorPos(1, sizeY)
- clearLine()
- clearWarningTick = -1
- end
- end
- ----------- Formatting & popups
- function FormatFloat(value, nbchar)
- local str = "?"
- if value ~= nil then
- str = string.format("%g", value)
- end
- if nbchar ~= nil then
- str = string.sub(" " .. str, -nbchar)
- end
- return str
- end
- function FormatInteger(value, nbchar)
- local str = "?"
- if value ~= nil then
- str = string.format("%d", value)
- end
- if nbchar ~= nil then
- str = string.sub(" " .. str, -nbchar)
- end
- return str
- end
- function boolToYesNo(bool)
- if bool then
- return "YES"
- else
- return "no"
- end
- end
- function readInputNumber(currentValue)
- local inputAbort = false
- local input = string.format(currentValue)
- if input == "0" then
- input = ""
- end
- local x, y = term.getCursorPos()
- repeat
- ClearWarning()
- SetColorDefault()
- SetCursorPos(x, y)
- Write(input .. " ")
- input = string.sub(input, -9)
- local params = { os.pullEventRaw() }
- local eventName = params[1]
- local address = params[2]
- if address == nil then address = "none" end
- if eventName == "key" then
- local keycode = params[2]
- if keycode >= 2 and keycode <= 10 then -- 1 to 9
- input = input .. string.format(keycode - 1)
- elseif keycode == 11 or keycode == 82 then -- 0 & keypad 0
- input = input .. "0"
- elseif keycode >= 79 and keycode <= 81 then -- keypad 1 to 3
- input = input .. string.format(keycode - 78)
- elseif keycode >= 75 and keycode <= 77 then -- keypad 4 to 6
- input = input .. string.format(keycode - 71)
- elseif keycode >= 71 and keycode <= 73 then -- keypad 7 to 9
- input = input .. string.format(keycode - 64)
- elseif keycode == 14 then -- Backspace
- input = string.sub(input, 1, string.len(input) - 1)
- elseif keycode == 211 then -- Delete
- input = ""
- elseif keycode == 28 then -- Enter
- inputAbort = true
- elseif keycode == 74 or keycode == 12 or keycode == 49 then -- - on numeric keypad or - on US top or n letter
- if string.sub(input, 1, 1) == "-" then
- input = string.sub(input, 2)
- else
- input = "-" .. input
- end
- elseif char == 43 or keycode == 78 then -- +
- if string.sub(input, 1, 1) == "-" then
- input = string.sub(input, 2)
- end
- else
- ShowWarning("Key " .. keycode .. " is invalid")
- end
- elseif eventName == "terminate" then
- inputAbort = true
- elseif not common_event(eventName, params[2]) then
- ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
- end
- until inputAbort
- SetCursorPos(1, y + 1)
- if input == "" or input == "-" then
- return currentValue
- else
- return tonumber(input)
- end
- end
- function readInput(currentValue)
- local inputAbort = false
- local input = string.format(currentValue)
- local x, y = term.getCursorPos()
- Write(input)
- os.pullEventRaw() -- skip first char event
- repeat
- ClearWarning()
- SetColorDefault()
- SetCursorPos(x, y)
- Write(input .. " ")
- input = string.sub(input, -123)
- local params = { os.pullEventRaw() }
- local eventName = params[1]
- local address = params[2]
- if address == nil then address = "none" end
- if eventName == "key" then
- local keycode = params[2]
- if keycode == 14 then -- Backspace
- input = string.sub(input, 1, string.len(input) - 1)
- elseif keycode == 211 then -- Delete
- input = ""
- elseif keycode == 28 then -- Enter
- inputAbort = true
- end
- elseif eventName == "char" then
- local char = params[2]
- if char >= ' ' and char <= '~' then -- 1 to 9
- input = input .. char
- end
- elseif eventName == "terminate" then
- inputAbort = true
- elseif eventName == "paste" then
- input = params[2]
- elseif not common_event(eventName, params[2]) then
- ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
- end
- until inputAbort
- SetCursorPos(1, y + 1)
- if input == "" then
- return currentValue
- else
- return input
- end
- end
- function readInputText(currentValue)
- local inputAbort = false
- local input = string.format(currentValue)
- local x, y = term.getCursorPos()
- Write(input)
- os.pullEventRaw() -- skip first char event
- repeat
- ClearWarning()
- SetColorDefault()
- SetCursorPos(x, y)
- Write(input .. " ")
- input = string.sub(input, -30)
- local params = { os.pullEventRaw() }
- local eventName = params[1]
- local address = params[2]
- if address == nil then address = "none" end
- if eventName == "key" then
- local keycode = params[2]
- if keycode == 14 then -- Backspace
- input = string.sub(input, 1, string.len(input) - 1)
- elseif keycode == 211 then -- Delete
- input = ""
- elseif keycode == 28 then -- Enter
- inputAbort = true
- else
- ShowWarning("Key " .. keycode .. " is invalid")
- end
- elseif eventName == "char" then
- local char = params[2]
- if char >= ' ' and char <= '~' then -- 1 to 9
- input = input .. char
- else
- ShowWarning("Char #" .. string.byte(char) .. " is invalid")
- end
- elseif eventName == "terminate" then
- inputAbort = true
- elseif not common_event(eventName, params[2]) then
- ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
- end
- until inputAbort
- SetCursorPos(1, y + 1)
- if input == "" then
- return currentValue
- else
- return input
- end
- end
- function readConfirmation(msg)
- if msg == nil then
- ShowWarning("Are you sure? (y/n)")
- else
- ShowWarning(msg)
- end
- repeat
- local params = { os.pullEventRaw() }
- local eventName = params[1]
- local address = params[2]
- if address == nil then address = "none" end
- if eventName == "key" then
- local keycode = params[2]
- if keycode == 21 then -- Y
- return true
- else
- return false
- end
- elseif eventName == "terminate" then
- return false
- elseif not common_event(eventName, params[2]) then
- ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
- end
- until false
- end
- ----------- commons: menu, event handlers, etc.
- function common_event(eventName, param)
- if eventName == "redstone" then
- redstone_event(param)
- elseif eventName == "timer" then
- if param == radar_timerId then
- radar_timerEvent()
- end
- elseif eventName == "shipCoreCooldownDone" then
- ShowWarning("Ship core cooldown done")
- elseif eventName == "reactorPulse" then
- reactor_pulse(param)
- -- elseif eventName == "reactorDeactivation" then
- -- ShowWarning("Reactor deactivated")
- -- elseif eventName == "reactorActivation" then
- -- ShowWarning("Reactor activated")
- elseif eventName == "char" then
- elseif eventName == "key_up" then
- elseif eventName == "mouse_click" then
- elseif eventName == "mouse_up" then
- elseif eventName == "mouse_drag" then
- elseif eventName == "monitor_touch" then
- elseif eventName == "monitor_resize" then
- elseif eventName == "peripheral" then
- elseif eventName == "peripheral_detach" then
- else
- return false
- end
- return true
- end
- function menu_common()
- SetCursorPos(1, 17)
- SetColorFooter()
- ShowMenu("0 Home 1 Ship 2 Cloak 3 Shield 4 Radar 5 Mining ")
- ShowMenu("6 Transport 7 Reactor 8 Hooks X eXit ")
- SetColorDefault()
- end
- ----------- Redstone support
- local tblRedstoneState = {-- Remember redstone state on each side
- ["top"] = rs.getInput("top"),
- ["front"] = rs.getInput("front"),
- ["left"] = rs.getInput("left"),
- ["right"] = rs.getInput("right"),
- ["back"] = rs.getInput("back"),
- ["bottom"] = rs.getInput("bottom"),
- }
- local tblSides = {-- list all sides and offset coordinates
- ["top" ] = { 3, 1},
- ["front" ] = { 1, 3},
- ["left" ] = { 3, 3},
- ["right" ] = { 5, 3},
- ["back" ] = { 5, 5},
- ["bottom"] = { 3, 5},
- }
- function redstone_event()
- -- Event only returns nil so we need to check sides manually
- local message = ""
- for side, state in pairs(tblRedstoneState) do
- if rs.getInput(side) ~= state then
- -- print(side .. " is now " .. tostring(rs.getInput(side)))
- message = message .. side .. " "
- tblRedstoneState[side] = rs.getInput(side)
- end
- end
- if message ~= "" then
- message = "Redstone changed on " .. message
- showWarning(message)
- end
- end
- ----------- Configuration
- function data_save()
- local file = fs.open("shipdata.txt", "w")
- if file ~= nil then
- file.writeLine(textutils.serialize(data))
- file.close()
- else
- ShowWarning("No file system")
- os.sleep(3)
- end
- end
- function data_read()
- data = { }
- if fs.exists("shipdata.txt") then
- local file = fs.open("shipdata.txt", "r")
- data = textutils.unserialize(file.readAll())
- file.close()
- if data == nil then data = {}; end
- end
- if data.radar_monitorIndex == nil then data.radar_monitorIndex = 0; end
- if data.radar_radius == nil then data.radar_radius = 500; end
- if data.radar_autoscan == nil then data.radar_autoscan = false; end
- if data.radar_autoscanDelay == nil then data.radar_autoscanDelay = 3; end
- if data.radar_results == nil then data.radar_results = {}; end
- if data.radar_scale == nil then data.radar_scale = 500; end
- if data.radar_offsetX == nil then data.radar_offsetX = 0; end
- if data.radar_offsetY == nil then data.radar_offsetY = 0; end
- if data.radar_hook == nil then data.radar_hook = 0; end
- end
- function string_split(source, sep)
- local sep = sep or ":"
- local fields = {}
- local pattern = string.format("([^%s]+)", sep)
- source:gsub(pattern, function(c) fields[#fields + 1] = c end)
- return fields
- end
- ----------- Radar support
- radar_listOffset = 0
- radar_timerId = -1
- radar_timerLength = 1
- radar_x = 0
- radar_y = 0
- radar_z = 0
- function radar_boot()
- if radar ~= nil then
- WriteLn("Booting Radar...")
- if data.radar_monitorIndex > 0 then
- local _, _, radar_x, radar_y, radar_z = radar.getGlobalPosition()
- radar_drawMap()
- end
- if data.radar_autoscan then
- radar.enable()
- radar_scan()
- end
- end
- end
- function radar_key(char, keycode)
- if char == 83 or char == 115 or keycode == 31 then -- S
- data.radar_autoscan = false
- radar_scan()
- return true
- elseif keycode == 30 then -- A
- data.radar_autoscan = true
- radar_scan()
- return true
- elseif char == 80 or char == 112 or keycode == 25 then -- P
- data.radar_autoscan = false
- return true
- elseif keycode == 49 then -- N
- radar_setMonitorIndex(data.radar_monitorIndex + 1)
- data_save()
- radar_drawMap()
- return true
- elseif char == 71 or char == 103 or keycode == 34 then -- G
- radar_setRadius(data.radar_radius - 100)
- data_save()
- return true
- elseif char == 84 or char == 116 or keycode == 20 then -- T
- if data.radar_radius < 100 then
- radar_setRadius(100)
- else
- radar_setRadius(data.radar_radius + 100)
- end
- data_save()
- return true
- elseif keycode == 36 then -- J
- radar_listOffset = math.max(0, radar_listOffset - 3)
- return true
- elseif keycode == 22 then -- U
- radar_listOffset = math.min(#data.radar_results - 1, radar_listOffset + 3)
- return true
- elseif char == 45 or keycode == 74 then -- -
- data.radar_scale = math.min(10000, math.floor(data.radar_scale * 1.2 + 0.5))
- data_save()
- radar_drawMap()
- return true
- elseif char == 43 or keycode == 78 then -- +
- data.radar_scale = math.max(20, math.floor(data.radar_scale * 0.8 + 0.5))
- data_save()
- radar_drawMap()
- return true
- elseif keycode == 199 then -- home
- data.radar_offsetX = 0
- data.radar_offsetY = 0
- data.radar_scale = 20
- for i = 0, #data.radar_results - 1 do
- data.radar_scale = math.max(data.radar_scale, math.max(math.abs(radar_x - data.radar_results[i].x), math.abs(radar_z - data.radar_results[i].z)))
- end
- data.radar_scale = math.min(10000, math.floor(data.radar_scale * 1.1 + 0.5))
- data_save()
- radar_drawMap()
- return true
- elseif keycode == 203 then -- left
- data.radar_offsetX = data.radar_offsetX - 0.20 * data.radar_scale
- data_save()
- radar_drawMap()
- return true
- elseif keycode == 205 then -- right
- data.radar_offsetX = data.radar_offsetX + 0.20 * data.radar_scale
- data_save()
- radar_drawMap()
- return true
- elseif keycode == 200 then -- up
- data.radar_offsetY = data.radar_offsetY - 0.20 * data.radar_scale
- data_save()
- radar_drawMap()
- return true
- elseif keycode == 208 then -- down
- data.radar_offsetY = data.radar_offsetY + 0.20 * data.radar_scale
- data_save()
- radar_drawMap()
- return true
- elseif keycode == 46 then -- C
- radar_page_config()
- data_save()
- return true
- end
- return false
- end
- function radar_page()
- local radar_resultsPerPage = 9
- ShowTitle(label .. " - Radar map")
- SetCursorPos(1, 2)
- if radar == nil or radar.isInterfaced() == nil then
- SetColorDisabled()
- Write("Radar not detected")
- else
- SetColorDefault()
- if #data.radar_results == 0 then
- Write("No contacts in range...")
- else
- local lastResultShown = radar_listOffset + radar_resultsPerPage - 1
- if lastResultShown >= #data.radar_results then
- lastResultShown = #data.radar_results - 1
- end
- Write("Displaying results " .. (radar_listOffset + 1) .. " to " .. (lastResultShown + 1) .. " of " .. #data.radar_results)
- for i = radar_listOffset, lastResultShown do
- SetCursorPos(4, 3 + i - radar_listOffset)
- if data.radar_results ~= nil then
- Write(FormatInteger(data.radar_results[i].x, 7) .. ", " .. FormatInteger(data.radar_results[i].y, 4) .. ", " .. FormatInteger(data.radar_results[i].z, 7))
- else
- Write("~nil~")
- end
- end
- end
- SetColorDefault()
- local energyStored, energyMax, energyUnits = radar.getEnergyStatus()
- local success, result = radar.getEnergyRequired(data.radar_radius)
- local energyRequired = 0
- if success then
- energyRequired = result
- end
- SetCursorPos(1, 12)
- Write("Energy: ")
- if energyStored > energyRequired then
- SetColorSuccess()
- else
- SetColorWarning()
- end
- Write(FormatInteger(energyStored, 10) .. " " .. energyUnits)
- SetColorDefault()
- SetCursorPos(1, 13)
- Write("Radius: " .. data.radar_radius)
- SetCursorPos(30, 13)
- Write("Monitor# " .. data.radar_monitorIndex .. "/" .. #monitors)
- SetCursorPos(1, 14)
- Write("Autoscan: ")
- if data.radar_autoscan then SetColorSuccess() else SetColorDefault() end
- Write(boolToYesNo(data.radar_autoscan))
- SetColorDefault()
- SetCursorPos(30, 14)
- Write("Delay " .. data.radar_autoscanDelay .. "s")
- end
- SetColorFooter()
- SetCursorPos(1, 15)
- ShowMenu(" S - Scan once, A - Autoscan, P - Stop autoscan")
- SetCursorPos(1, 16)
- ShowMenu(" T/G - Scan radius, U/J - Scroll list")
- SetCursorPos(1, 17)
- ShowMenu(" +/- - Scale, N - Next monitor, C - Configuration")
- end
- function radar_page_config()
- ShowTitle(label .. " - Radar configuration")
- SetCursorPos(1, 2)
- if radar == nil or radar.isInterfaced() == nil then
- SetColorDisabled()
- Write("No radar detected")
- else
- SetColorDefault()
- SetCursorPos(1, 3)
- Write("Radar scan radius (" .. data.radar_radius .. " blocks): ")
- radar_setRadius(readInputNumber(data.radar_radius))
- SetCursorPos(1, 5)
- Write("Autoscan delay in seconds (" .. data.radar_autoscanDelay .. "): ")
- radar_setAutoscanDelay(readInputNumber(data.radar_autoscanDelay))
- SetCursorPos(1, 7)
- Write("Output monitor (" .. data.radar_monitorIndex .. "/" .. #monitors .. "): ")
- radar_setMonitorIndex(readInputNumber(data.radar_monitorIndex))
- data_save()
- end
- end
- function hooks_page_config()
- ShowTitle(label .. " - Webhook configuration")
- SetCursorPos(1, 2)
- SetColorDefault()
- SetCursorPos(1, 3)
- Write("Ship GPS Webhook: ")
- hook_gpsset(readInput(data.gps_hook))
- SetCursorPos(1, 5)
- Write("Radar Results Webhook: ")
- hook_radset(readInput(data.radar_hook))
- SetCursorPos(1, 7)
- Write("AutoPilot Webhook: ")
- hook_autoset(readInput(data.auto_hook))
- data_save()
- end
- function hook_gpsset(newGps)
- data.gps_hook = newGps
- end
- function hook_radset(newRad)
- data.radar_hook = newRad
- end
- function hook_autoset(newAuto)
- data.auto_hook = newAuto
- end
- function radar_setRadius(newRadius)
- if newRadius < 1 then
- data.radar_radius = 1
- elseif newRadius >= 10000 then
- data.radar_radius = 10000
- else
- data.radar_radius = newRadius
- end
- end
- function radar_setAutoscanDelay(newAutoscanDelay)
- if newAutoscanDelay < 1 then
- data.radar_autoscanDelay = 1
- elseif newAutoscanDelay >= 3600 then -- 1 hour
- data.radar_autoscanDelay = 3600
- else
- data.radar_autoscanDelay = newAutoscanDelay
- end
- end
- function radar_setMonitorIndex(newIndex)
- if #monitors == 0 or newIndex < 0 or newIndex > #monitors then
- data.radar_monitorIndex = 0
- else
- data.radar_monitorIndex = newIndex
- end
- end
- function radar_getMonitor()
- if data.radar_monitorIndex > 0 and data.radar_monitorIndex <= #monitors then
- return monitors[data.radar_monitorIndex]
- else
- return nil
- end
- end
- function radar_scan()
- local monitor = radar_getMonitor()
- if radar == nil or radar.isInterfaced() == nil then
- draw_warning(monitor, "No radar")
- return false
- end
- local energyStored, energyMax, _ = radar.getEnergyStatus()
- if energyStored == nil then energyStored = 0 end
- if energyMax == nil or energyMax == 0 then energyMax = 1 end
- radar.radius(radius)
- radar.start()
- local success, result = radar.getEnergyRequired()
- if not success then
- showErrorAndExit(result)
- end
- local energyRequired = result
- if energyRequired <= 0 or energyStored < energyRequired then
- textOut((w / 2) - 7, 1, " /!\\ LOW POWER ", colors.white, colors.red)
- os.sleep(1)
- return 0
- end
- if radar_timerId ~= -1 and radar.getResultsCount() == -1 then
- draw_warning(monitor, "Already scanning...")
- return false
- end
- local scanDuration = radar.getScanDuration()
- radar_timerId = os.startTimer(radar_timerLength)
- os.sleep(scanDuration)
- radar.radius(data.radar_radius)
- if radar.start() then
- draw_warning(monitor, "Scanning...")
- end
- return false
- end
- function radar_scanDone()
- local numResults = radar.getResultsCount()
- data.radar_results = {}
- if (numResults ~= 0) then
- for i = 0, numResults do
- local success, type, name, x, y, z = radar.getResult(i)
- if success then
- if name == "" then
- name = "?"
- end
- data.radar_results[i] = { x = x, y = y, z = z, name = name, type = type }
- end
- end
- data.radar_scale = data.radar_radius
- end
- data_save()
- radar_drawMap()
- end
- function draw_text(monitor, x, y, text, textColor, backgroundColor)
- if monitor == nil then
- term.setCursorPos(x, y)
- if textColor ~= nil then term.setTextColor(textColor) end
- if backgroundColor ~= nil then term.setBackgroundColor(backgroundColor) end
- term.write(text)
- local xt, yt = term.getCursorPos()
- term.setCursorPos(1, yt + 1)
- else
- monitor.setCursorPos(x, y)
- if textColor ~= nil then monitor.setTextColor(textColor) end
- if backgroundColor ~= nil then monitor.setBackgroundColor(backgroundColor) end
- monitor.write(text)
- local xt, yt = monitor.getCursorPos()
- monitor.setCursorPos(1, yt + 1)
- end
- end
- function draw_warning(monitor, text)
- local screenWidth, screenHeight
- if monitor == nil then
- screenWidth, screenHeight = term.getSize()
- else
- screenWidth, screenHeight = monitor.getSize()
- end
- local centerX = math.floor(screenWidth / 2)
- local centerY = math.floor(screenHeight / 2)
- local halfWidth = math.ceil(string.len(text) / 2)
- local blank = string.sub(" ", - (string.len(text) + 2))
- draw_text(monitor, centerX - halfWidth - 1, centerY - 1, blank, colors.white, colors.red)
- draw_text(monitor, centerX - halfWidth - 1, centerY , " " .. text .. " ", colors.white, colors.red)
- draw_text(monitor, centerX - halfWidth - 1, centerY + 1, blank, colors.white, colors.red)
- end
- function draw_centeredText(monitor, y, text)
- local screenWidth, screenHeight
- if monitor == nil then
- screenWidth, screenHeight = term.getSize()
- else
- screenWidth, screenHeight = monitor.getSize()
- end
- local x = math.floor(screenWidth / 2 - string.len(text) / 2 + 0.5)
- draw_text(monitor, x, y, text, nil, nil)
- end
- function radar_drawContact(monitor, contact)
- local screenWidth, screenHeight
- if monitor == nil then
- screenWidth, screenHeight = term.getSize()
- else
- screenWidth, screenHeight = monitor.getSize()
- end
- local screenX = (radar_x + data.radar_offsetX - contact.x) / data.radar_scale
- local screenY = (radar_z + data.radar_offsetY - contact.z) / data.radar_scale
- local visible = true
- if screenX <= -1 or screenX >= 1 or screenY <= -1 or screenY >= 1 then
- screenX = math.min(1, math.max(-1, screenX))
- screenY = math.min(1, math.max(-1, screenY))
- visible = false
- end
- screenX = math.floor(screenX * (screenWidth - 3) / 2 + ((screenWidth - 1) / 2) + 1.5)
- screenY = math.floor(screenY * (screenHeight - 3) / 2 + ((screenHeight - 1) / 2) + 1.5)
- if contact.type == "self" then
- draw_text(monitor, screenX, screenY, Style.TextRadarself, Style.CRadarself, Style.BGRadarself)
- else
- draw_text(monitor, screenX, screenY, Style.TextRadarother, Style.CRadarother, Style.BGRadarother)
- end
- if visible then
- local text = contact.name
- screenX = math.min(screenWidth - 1 - string.len(text), math.max(2, math.floor(screenX - string.len(text) / 2 + 0.5)))
- if screenY == (screenHeight - 1) then
- screenY = screenY - 1
- else
- screenY = screenY + 1
- end
- draw_text(monitor, screenX, screenY, text, Style.CRadarother, Style.BGRadarother)
- end
- end
- function radar_drawMap()
- local screenWidth, screenHeight, x, y
- local monitor = radar_getMonitor()
- -- center area
- SetColorRadarmap()
- if monitor == nil then
- term.clear()
- screenWidth, screenHeight = term.getSize()
- else
- monitor.clear()
- screenWidth, screenHeight = monitor.getSize()
- end
- -- borders
- SetColorRadarborder()
- for x = 1, screenWidth do
- if monitor == nil then
- term.setCursorPos(x, 1)
- term.write(" ")
- term.setCursorPos(x, screenHeight)
- term.write(" ")
- else
- monitor.setCursorPos(x, 1)
- monitor.write(" ")
- monitor.setCursorPos(x, screenHeight)
- monitor.write(" ")
- end
- end
- for y = 2, screenHeight - 1 do
- if monitor == nil then
- term.setCursorPos(1, y)
- term.write(" ")
- term.setCursorPos(screenWidth, y)
- term.write(" ")
- else
- monitor.setCursorPos(1, y)
- monitor.write(" ")
- monitor.setCursorPos(screenWidth, y)
- monitor.write(" ")
- end
- end
- -- title
- local text = label .. " - Radar map"
- if #data.radar_results == 0 then
- text = text .. " (no contacts)"
- else
- text = text .. " (" .. #data.radar_results .. " contacts)"
- end
- draw_centeredText(monitor, 1, text)
- -- status
- local text = "Scan radius: " .. data.radar_radius
- if radar ~= nil then
- local energyStored, energyMax, energyUnits = radar.getEnergyStatus()
- text = text .. " | Energy: " .. energyStored .. " " .. energyUnits
- end
- text = text .. " | Scale: " .. data.radar_scale
- draw_centeredText(monitor, screenHeight, text)
- -- results
- SetCursorPos(1, 12)
- radar_drawContact(monitor, {x = radar_x, y = radar_y, z = radar_z, name = "", type = "self"})
- for i = 0, #data.radar_results - 1 do
- radar_drawContact(monitor, data.radar_results[i])
- end
- -- restore defaults
- SetColorDefault()
- end
- radar_waitingNextScan = false
- function radar_timerEvent()
- radar_timerId = -1
- if radar_waitingNextScan then
- radar_waitingNextScan = false
- radar_scan() -- will restart timer
- else
- local numResults = radar.getResultsCount()
- if numResults ~= -1 then
- radar_scanDone()
- if data.radar_autoscan then
- radar_waitingNextScan = true
- radar_timerId = os.startTimer(data.radar_autoscanDelay)
- end
- else -- still scanning
- -- radar_timerId = os.startTimer(data.radar_autoscanDelay)
- end
- end
- end
- ----------- Boot sequence
- math.randomseed(os.time())
- label = os.getComputerLabel()
- if not label then
- label = "" .. os.getComputerID()
- end
- -- read configuration
- data_read()
- clear()
- print("data_read...")
- -- initial scanning
- monitors = {}
- ShowTitle(label .. " - Connecting...")
- WriteLn("")
- sides = peripheral.getNames()
- radar = nil
- for key,side in pairs(sides) do
- os.sleep(0)
- Write("Checking " .. side .. " ")
- local componentType = peripheral.getType(side)
- Write(componentType .. " ")
- if componentType == "warpdriveRadar" then
- Write("wrapping!")
- radar = peripheral.wrap(side)
- elseif componentType == "monitor" then
- Write("wrapping!")
- lmonitor = peripheral.wrap(side)
- table.insert(monitors, lmonitor)
- lmonitor.setTextScale(monitor_textScale)
- end
- WriteLn("")
- end
- if not os.getComputerLabel() and (ship ~= nil or reactor ~= nil) then
- data_setName()
- end
- -- peripherals status
- function connections_page()
- SetCursorPos(0, 1)
- SetColorTitle()
- ShowTitle(label .. " - Connections")
- WriteLn("")
- if #monitors == 0 then
- SetColorWarning()
- WriteLn("No Monitor detected")
- elseif #monitors == 1 then
- SetColorSuccess()
- WriteLn("1 monitor detected")
- else
- SetColorSuccess()
- WriteLn(#monitors .. " Monitors detected")
- end
- if radar == nil or radar.isInterfaced() == nil then
- SetColorWarning()
- WriteLn("No radar detected")
- else
- SetColorSuccess()
- WriteLn("Radar detected")
- end
- WriteLn("")
- SetColorDefault()
- WriteLn("Please refer to below menu for keyboard controls")
- WriteLn("For example, press 1 to access Ship page")
- end
- -- peripheral boot up
- clear()
- connections_page()
- SetColorDefault()
- WriteLn("")
- os.sleep(0)
- radar_boot()
- os.sleep(0)
- -- main loop
- abort = false
- refresh = true
- page = connections_page
- keyHandler = nil
- repeat
- ClearWarning()
- if refresh then
- clear()
- page()
- menu_common()
- refresh = false
- end
- params = { os.pullEventRaw() }
- eventName = params[1]
- address = params[2]
- if address == nil then address = "none" end
- -- WriteLn("...")
- -- WriteLn("Event '" .. eventName .. "', " .. address .. ", " .. params[3] .. ", " .. params[4] .. " received")
- -- os.sleep(0.2)
- if eventName == "key" then
- keycode = params[2]
- if char == 88 or char == 120 or keycode == 45 then -- x for eXit
- os.pullEventRaw()
- abort = true
- elseif char == 48 or keycode == 11 or keycode == 82 then -- 0
- page = connections_page
- keyHandler = nil
- refresh = true
- elseif char == 49 or keycode == 2 or keycode == 79 then -- 1
- page = core_page
- keyHandler = core_key
- refresh = true
- elseif char == 50 or keycode == 3 or keycode == 80 then -- 2
- page = cloaking_page
- keyHandler = cloaking_key
- refresh = true
- elseif char == 51 or keycode == 4 or keycode == 81 then -- 3
- page = shield_page
- keyHandler = shield_key
- refresh = true
- elseif char == 52 or keycode == 5 or keycode == 75 then -- 4
- page = radar_page
- keyHandler = radar_key
- refresh = true
- elseif char == 53 or keycode == 6 or keycode == 76 then -- 5
- page = mining_page
- keyHandler = mining_key
- refresh = true
- elseif char == 54 or keycode == 7 or keycode == 77 then -- 6
- page = mining_page
- keyHandler = mining_key
- refresh = true
- elseif char == 55 or keycode == 8 then -- 7
- page = reactor_page
- keyHandler = reactor_key
- refresh = true
- elseif char == 56 or keycode == 9 or keycode == 79 then -- 8
- page = hooks_page_config
- keyHandler = hooks_page_key
- refresh = true
- elseif keyHandler ~= nil and keyHandler(char, keycode) then
- refresh = true
- os.sleep(0)
- elseif char == 0 then -- control chars
- refresh = false
- os.sleep(0)
- else
- ShowWarning("Key " .. keycode .. " is invalid")
- os.sleep(0.2)
- end
- elseif eventName == "terminate" then
- abort = true
- elseif not common_event(eventName, params[2]) then
- ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
- refresh = true
- os.sleep(0.2)
- end
- until abort
- -- clear screens on exit
- SetMonitorColorFrontBack(colors.white, colors.black)
- term.clear()
- if monitors ~= nil then
- for key,monitor in pairs(monitors) do
- monitor.clear()
- end
- end
- SetCursorPos(1, 1)
- WriteLn("Program terminated")
- WriteLn("Type startup to restart it")
Add Comment
Please, Sign In to add comment