Guest User

radar.lua

a guest
Apr 27th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.18 KB | None | 0 0
  1. --Made by GAPLS
  2. local basalt = require("basalt")
  3.  
  4. --Scanning
  5. local shipData = {}
  6. local radar = peripheral.find("radar")
  7. local updateInterval = 1
  8.  
  9. --Wireless
  10. local modem = peripheral.find("modem")
  11. local frequency = 49
  12. local maxDistance = 255
  13.  
  14. --Display Localy
  15. local monitor = peripheral.find("monitor")
  16.  
  17. --Needs to be sent as well to get it displayed correctly
  18. local hostPosition = {7, 106, 31}
  19. local ratio = 0.2
  20. local dir = 0 --0=North | 1=East | 2=South | 3=West
  21.  
  22. local screenSizeMon = {}
  23. local screenCenterMon = {}
  24. local screenSizeTerm = {}
  25. local screenCenterTerm = {}
  26.  
  27. function SetMonitor(mon)
  28.     local screenWidth, screenHeight = mon.getSize()
  29.     screenSizeMon = {screenWidth, screenHeight}
  30.     screenCenterMon = {screenWidth * 0.5, screenHeight * 0.5}
  31. end
  32.  
  33. function SetTerminal(terminal, isUI)
  34.     local screenWidht, screenHeight
  35.     if isUI == false then
  36.         screenWidht, screenHeight = terminal.getSize()
  37.     else
  38.         screenWidht, screenHeight = terminal:getSize()
  39.     end
  40.     screenSizeTerm = {screenWidht, screenHeight}
  41.     screenCenterTerm = {screenWidht * 0.5, screenHeight * 0.5}
  42. end
  43.  
  44. function WorldPosToTermPos(worldX, worldY, worldZ)
  45.         --Distance from the world position to the host multiplied by a ratio to squish/stratch position
  46.         local offsetX = (worldX - hostPosition[1]) * ratio
  47.         local offsetY = (worldZ - hostPosition[3]) * ratio
  48.  
  49.         --Translate the offsets to the screen where the center is the null point
  50.         local screenX = 0
  51.         local screenY = 0
  52.  
  53.         if dir == 0 then
  54.             --NORTH
  55.             screenX = math.floor(screenCenterTerm[1] + offsetX)
  56.             screenY = math.floor(screenCenterTerm[2] + offsetY)
  57.         elseif dir == 1 then
  58.             --EAST
  59.             screenX = math.floor(screenCenterTerm[1] + offsetY)
  60.             screenY = math.floor(screenCenterTerm[2] - offsetX)
  61.         elseif dir == 2 then
  62.             --SOUTH
  63.             screenX = math.floor(screenCenterTerm[1] - offsetX)
  64.             screenY = math.floor(screenCenterTerm[2] - offsetY)
  65.         else
  66.             --WEST
  67.             screenX = math.floor(screenCenterTerm[1] - offsetY)
  68.             screenY = math.floor(screenCenterTerm[2] + offsetX)
  69.         end
  70.  
  71.         term.setCursorPos(screenX, screenY)
  72.         term.write("*")
  73. end
  74.  
  75. function WorldPosToMonitorPos(worldX, worldY, worldZ)
  76.     --Distance from the world position to the host multiplied by a ratio to squish/stratch position
  77.     local offsetX = (worldX - hostPosition[1]) * ratio
  78.     local offsetY = (worldZ - hostPosition[3]) * ratio
  79.  
  80.     --Translate the offsets to the screen where the center is the null point
  81.     local screenX = 0
  82.     local screenY = 0
  83.  
  84.     if dir == 0 then
  85.         --NORTH
  86.         screenX = math.floor(screenCenterMon[1] + offsetX)
  87.         screenY = math.floor(screenCenterMon[2] + offsetY)
  88.     elseif dir == 1 then
  89.         --EAST
  90.         screenX = math.floor(screenCenterMon[1] + offsetY)
  91.         screenY = math.floor(screenCenterMon[2] - offsetX)
  92.     elseif dir == 2 then
  93.         --SOUTH
  94.         screenX = math.floor(screenCenterMon[1] - offsetX)
  95.         screenY = math.floor(screenCenterMon[2] - offsetY)
  96.     else
  97.         --WEST
  98.         screenX = math.floor(screenCenterMon[1] - offsetY)
  99.         screenY = math.floor(screenCenterMon[2] + offsetX)
  100.     end
  101.  
  102.     monitor.setCursorPos(screenX, screenY)
  103.     monitor.write("*")
  104. end
  105.  
  106. function Preview()
  107.     term.clear()
  108.     term.setCursorPos(screenCenterTerm[1], screenCenterTerm[2])
  109.     term.write("#")
  110.  
  111.     while true do
  112.         for _, value in pairs(shipData) do
  113.             local boatX = math.floor(value["position"]["x"])
  114.             local boatY = math.floor(value["position"]["y"])
  115.             local boatZ = math.floor(value["position"]["z"])
  116.             WorldPosToTermPos(boatX, boatY, boatZ)
  117.         end
  118.         os.sleep(updateInterval)
  119.     end
  120. end
  121.  
  122. function Display()
  123.     if monitor == nil or onlyTerm == true then
  124.         term.clear()
  125.         term.setCursorPos(screenCenterTerm[1], screenCenterTerm[2])
  126.         term.write("#")
  127.     else
  128.         monitor.clear()
  129.         monitor.setCursorPos(screenCenterMon[1], screenCenterMon[2])
  130.         monitor.write("#")
  131.     end
  132.  
  133.     for _, value in pairs(shipData) do
  134.         local boatX = math.floor(value["position"]["x"])
  135.         local boatY = math.floor(value["position"]["y"])
  136.         local boatZ = math.floor(value["position"]["z"])
  137.         if monitor == nil or onlyTerm then
  138.             WorldPosToTermPos(boatX, boatY, boatZ)
  139.         else
  140.             WorldPosToMonitorPos(boatX, boatY, boatZ)
  141.         end
  142.     end
  143. end
  144.  
  145. function ScanForShip()
  146.     while true do
  147.         local data = radar.scan(256)[1]
  148.  
  149.         if type(data) ~= "string" then
  150.             shipData = data
  151.         else
  152.             shipData = {}
  153.         end
  154.         Display(false)
  155.         os.sleep(updateInterval)
  156.     end
  157. end
  158.  
  159. --Respond to given frequency with ship data
  160. function WaitForRednet()
  161.     modem.open(frequency)
  162.  
  163.     while true do
  164.         local event, _, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  165.  
  166.         local response = {shipData, hostPosition, ratio, dir}
  167.  
  168.         if distance <= maxDistance then
  169.             modem.transmit(replyChannel, frequency, response)
  170.         end
  171.     end
  172. end
  173.  
  174. local main = basalt.createFrame()
  175. local thread_rednet = main:addThread()
  176. local thread_scan = main:addThread()
  177.  
  178. local preview = main:addProgram()
  179. :setPosition(20, 5)
  180.  
  181. local inp_x = main:addInput()
  182. :setDefaultText("X:")
  183. :setBackground(colors.green)
  184. :setInputType("number")
  185. :setInputLimit(5)
  186.  
  187. local inp_y = main:addInput()
  188. :setDefaultText("Y:")
  189. :setBackground(colors.green)
  190. :setInputType("number")
  191. :setInputLimit(5)
  192. :setPosition(1, 3)
  193.  
  194. local inp_z = main:addInput()
  195. :setDefaultText("Z:")
  196. :setBackground(colors.green)
  197. :setInputType("number")
  198. :setInputLimit(5)
  199. :setPosition(1, 5)
  200.  
  201. local inp_frequency = main:addInput()
  202. :setDefaultText("Frequency:")
  203. :setBackground(colors.green)
  204. :setInputType("number")
  205. :setInputLimit(4)
  206. :setPosition(1, 7)
  207.  
  208. local inp_maxDistance = main:addInput()
  209. :setDefaultText("Distance:")
  210. :setBackground(colors.green)
  211. :setInputType("number")
  212. :setInputLimit(3)
  213. :setPosition(1, 9)
  214.  
  215. function On_Interval_Change(input)
  216.     if type(input:getValue()) == "string" then return end
  217.  
  218.     if input:getValue() > 5 then
  219.         input:setValue(5)
  220.     elseif input:getValue() <= 0.1 then
  221.         input:setValue(0.1)
  222.     end
  223. end
  224.  
  225. local inp_updateInterval = main:addInput()
  226. :setDefaultText("Scan Interval:")
  227. :setBackground(colors.green)
  228. :setInputType("number")
  229. :setInputLimit(4)
  230. :setPosition(1, 13)
  231. :onChange(On_Interval_Change)
  232.  
  233. function On_RatioChanged(input)
  234.     if type(input:getValue()) == "string" then return end
  235.  
  236.     if input:getValue() > 1 then
  237.         input:setValue(1)
  238.     elseif input:getValue() < 0 then
  239.         input:setValue(0.01)
  240.     end
  241. end
  242.  
  243. local inp_ratio = main:addInput()
  244. :setDefaultText("Ratio:")
  245. :setBackground(colors.green)
  246. :setInputType("number")
  247. :setInputLimit(4)
  248. :setPosition(1, 11)
  249. :onChange(On_RatioChanged)
  250.  
  251. local drop_dir = main:addDropdown()
  252. :addItem("North")
  253. :addItem("East")
  254. :addItem("South")
  255. :addItem("West")
  256. :setBackground(colors.green)
  257. :setPosition(1, 15)
  258.  
  259. function On_Btn_Confirm_Clicked()
  260.     if #tostring(inp_x:getValue()) <= 0 or #tostring(inp_y:getValue()) <= 0 or #tostring(inp_z:getValue()) <= 0 or
  261.     #tostring(inp_frequency:getValue()) <= 0 or #tostring(inp_maxDistance:getValue()) <= 0 or
  262.     #tostring(inp_ratio:getValue()) <= 0 or drop_dir:getItemIndex() == nil
  263.     then return end
  264.  
  265.     if monitor ~= nil then
  266.         SetMonitor(monitor)
  267.     end
  268.     SetTerminal(preview, true)
  269.  
  270.     frequency = inp_frequency:getValue()
  271.     maxDistance = inp_maxDistance:getValue()
  272.     hostPosition = {inp_x:getValue(), inp_y:getValue(), inp_z:getValue()}
  273.     ratio = inp_ratio:getValue()
  274.     updateInterval = inp_updateInterval:getValue()
  275.     dir = drop_dir:getItemIndex() - 1
  276.  
  277.     preview:execute(Preview)
  278.  
  279.     thread_rednet:start(WaitForRednet)
  280.     thread_scan:start(ScanForShip)
  281. end
  282.  
  283. local btn_confirm = main:addButton()
  284. :setText("Confirm")
  285. :setPosition(40, 1)
  286. :setSize(9, 1)
  287. :onClick(On_Btn_Confirm_Clicked)
  288.  
  289. basalt.autoUpdate()
Advertisement
Add Comment
Please, Sign In to add comment