Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Made by GAPLS
- local basalt = require("basalt")
- --Scanning
- local shipData = {}
- local radar = peripheral.find("radar")
- local updateInterval = 1
- --Wireless
- local modem = peripheral.find("modem")
- local frequency = 49
- local maxDistance = 255
- --Display Localy
- local monitor = peripheral.find("monitor")
- --Needs to be sent as well to get it displayed correctly
- local hostPosition = {7, 106, 31}
- local ratio = 0.2
- local dir = 0 --0=North | 1=East | 2=South | 3=West
- local screenSizeMon = {}
- local screenCenterMon = {}
- local screenSizeTerm = {}
- local screenCenterTerm = {}
- function SetMonitor(mon)
- local screenWidth, screenHeight = mon.getSize()
- screenSizeMon = {screenWidth, screenHeight}
- screenCenterMon = {screenWidth * 0.5, screenHeight * 0.5}
- end
- function SetTerminal(terminal, isUI)
- local screenWidht, screenHeight
- if isUI == false then
- screenWidht, screenHeight = terminal.getSize()
- else
- screenWidht, screenHeight = terminal:getSize()
- end
- screenSizeTerm = {screenWidht, screenHeight}
- screenCenterTerm = {screenWidht * 0.5, screenHeight * 0.5}
- end
- function WorldPosToTermPos(worldX, worldY, worldZ)
- --Distance from the world position to the host multiplied by a ratio to squish/stratch position
- local offsetX = (worldX - hostPosition[1]) * ratio
- local offsetY = (worldZ - hostPosition[3]) * ratio
- --Translate the offsets to the screen where the center is the null point
- local screenX = 0
- local screenY = 0
- if dir == 0 then
- --NORTH
- screenX = math.floor(screenCenterTerm[1] + offsetX)
- screenY = math.floor(screenCenterTerm[2] + offsetY)
- elseif dir == 1 then
- --EAST
- screenX = math.floor(screenCenterTerm[1] + offsetY)
- screenY = math.floor(screenCenterTerm[2] - offsetX)
- elseif dir == 2 then
- --SOUTH
- screenX = math.floor(screenCenterTerm[1] - offsetX)
- screenY = math.floor(screenCenterTerm[2] - offsetY)
- else
- --WEST
- screenX = math.floor(screenCenterTerm[1] - offsetY)
- screenY = math.floor(screenCenterTerm[2] + offsetX)
- end
- term.setCursorPos(screenX, screenY)
- term.write("*")
- end
- function WorldPosToMonitorPos(worldX, worldY, worldZ)
- --Distance from the world position to the host multiplied by a ratio to squish/stratch position
- local offsetX = (worldX - hostPosition[1]) * ratio
- local offsetY = (worldZ - hostPosition[3]) * ratio
- --Translate the offsets to the screen where the center is the null point
- local screenX = 0
- local screenY = 0
- if dir == 0 then
- --NORTH
- screenX = math.floor(screenCenterMon[1] + offsetX)
- screenY = math.floor(screenCenterMon[2] + offsetY)
- elseif dir == 1 then
- --EAST
- screenX = math.floor(screenCenterMon[1] + offsetY)
- screenY = math.floor(screenCenterMon[2] - offsetX)
- elseif dir == 2 then
- --SOUTH
- screenX = math.floor(screenCenterMon[1] - offsetX)
- screenY = math.floor(screenCenterMon[2] - offsetY)
- else
- --WEST
- screenX = math.floor(screenCenterMon[1] - offsetY)
- screenY = math.floor(screenCenterMon[2] + offsetX)
- end
- monitor.setCursorPos(screenX, screenY)
- monitor.write("*")
- end
- function Preview()
- term.clear()
- term.setCursorPos(screenCenterTerm[1], screenCenterTerm[2])
- term.write("#")
- while true do
- for _, value in pairs(shipData) do
- local boatX = math.floor(value["position"]["x"])
- local boatY = math.floor(value["position"]["y"])
- local boatZ = math.floor(value["position"]["z"])
- WorldPosToTermPos(boatX, boatY, boatZ)
- end
- os.sleep(updateInterval)
- end
- end
- function Display()
- if monitor == nil or onlyTerm == true then
- term.clear()
- term.setCursorPos(screenCenterTerm[1], screenCenterTerm[2])
- term.write("#")
- else
- monitor.clear()
- monitor.setCursorPos(screenCenterMon[1], screenCenterMon[2])
- monitor.write("#")
- end
- for _, value in pairs(shipData) do
- local boatX = math.floor(value["position"]["x"])
- local boatY = math.floor(value["position"]["y"])
- local boatZ = math.floor(value["position"]["z"])
- if monitor == nil or onlyTerm then
- WorldPosToTermPos(boatX, boatY, boatZ)
- else
- WorldPosToMonitorPos(boatX, boatY, boatZ)
- end
- end
- end
- function ScanForShip()
- while true do
- local data = radar.scan(256)[1]
- if type(data) ~= "string" then
- shipData = data
- else
- shipData = {}
- end
- Display(false)
- os.sleep(updateInterval)
- end
- end
- --Respond to given frequency with ship data
- function WaitForRednet()
- modem.open(frequency)
- while true do
- local event, _, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- local response = {shipData, hostPosition, ratio, dir}
- if distance <= maxDistance then
- modem.transmit(replyChannel, frequency, response)
- end
- end
- end
- local main = basalt.createFrame()
- local thread_rednet = main:addThread()
- local thread_scan = main:addThread()
- local preview = main:addProgram()
- :setPosition(20, 5)
- local inp_x = main:addInput()
- :setDefaultText("X:")
- :setBackground(colors.green)
- :setInputType("number")
- :setInputLimit(5)
- local inp_y = main:addInput()
- :setDefaultText("Y:")
- :setBackground(colors.green)
- :setInputType("number")
- :setInputLimit(5)
- :setPosition(1, 3)
- local inp_z = main:addInput()
- :setDefaultText("Z:")
- :setBackground(colors.green)
- :setInputType("number")
- :setInputLimit(5)
- :setPosition(1, 5)
- local inp_frequency = main:addInput()
- :setDefaultText("Frequency:")
- :setBackground(colors.green)
- :setInputType("number")
- :setInputLimit(4)
- :setPosition(1, 7)
- local inp_maxDistance = main:addInput()
- :setDefaultText("Distance:")
- :setBackground(colors.green)
- :setInputType("number")
- :setInputLimit(3)
- :setPosition(1, 9)
- function On_Interval_Change(input)
- if type(input:getValue()) == "string" then return end
- if input:getValue() > 5 then
- input:setValue(5)
- elseif input:getValue() <= 0.1 then
- input:setValue(0.1)
- end
- end
- local inp_updateInterval = main:addInput()
- :setDefaultText("Scan Interval:")
- :setBackground(colors.green)
- :setInputType("number")
- :setInputLimit(4)
- :setPosition(1, 13)
- :onChange(On_Interval_Change)
- function On_RatioChanged(input)
- if type(input:getValue()) == "string" then return end
- if input:getValue() > 1 then
- input:setValue(1)
- elseif input:getValue() < 0 then
- input:setValue(0.01)
- end
- end
- local inp_ratio = main:addInput()
- :setDefaultText("Ratio:")
- :setBackground(colors.green)
- :setInputType("number")
- :setInputLimit(4)
- :setPosition(1, 11)
- :onChange(On_RatioChanged)
- local drop_dir = main:addDropdown()
- :addItem("North")
- :addItem("East")
- :addItem("South")
- :addItem("West")
- :setBackground(colors.green)
- :setPosition(1, 15)
- function On_Btn_Confirm_Clicked()
- if #tostring(inp_x:getValue()) <= 0 or #tostring(inp_y:getValue()) <= 0 or #tostring(inp_z:getValue()) <= 0 or
- #tostring(inp_frequency:getValue()) <= 0 or #tostring(inp_maxDistance:getValue()) <= 0 or
- #tostring(inp_ratio:getValue()) <= 0 or drop_dir:getItemIndex() == nil
- then return end
- if monitor ~= nil then
- SetMonitor(monitor)
- end
- SetTerminal(preview, true)
- frequency = inp_frequency:getValue()
- maxDistance = inp_maxDistance:getValue()
- hostPosition = {inp_x:getValue(), inp_y:getValue(), inp_z:getValue()}
- ratio = inp_ratio:getValue()
- updateInterval = inp_updateInterval:getValue()
- dir = drop_dir:getItemIndex() - 1
- preview:execute(Preview)
- thread_rednet:start(WaitForRednet)
- thread_scan:start(ScanForShip)
- end
- local btn_confirm = main:addButton()
- :setText("Confirm")
- :setPosition(40, 1)
- :setSize(9, 1)
- :onClick(On_Btn_Confirm_Clicked)
- basalt.autoUpdate()
Advertisement
Add Comment
Please, Sign In to add comment