Advertisement
abstract_abstract

monitor.lua

Oct 9th, 2024
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.83 KB | None | 0 0
  1. local hostIDs = {32, 72, 95}
  2. local teleports = {}
  3. local textScale = 0.9
  4.  
  5.  
  6. function startOperations()
  7.     rednet.send(126, "", "dns_list")
  8.     local senderID, message, protocol = rednet.receive("dns_list_response")
  9.     teleports = textutils.unserialize(message)
  10.     for k, v in pairs(teleports) do
  11.         print(k, v)
  12.     end
  13. end
  14.  
  15.  
  16. function display()
  17.     monit = peripheral.wrap("top")
  18.     monit.setTextScale(textScale)
  19.     monit.setBackgroundColour(colors.blue)
  20.     monit.clear()
  21.     monit.setCursorPos(1, 1)
  22.     local function dispNewLine()
  23.         local x, y = monit.getCursorPos()
  24.         monit.setCursorPos(1, y + 1)
  25.     end
  26.  
  27.     local function displayLine()
  28.         local width = monit.getSize()
  29.         for i = 1, width do
  30.             monit.write("-")
  31.         end
  32.         dispNewLine()
  33.     end
  34.  
  35.     for k, v in pairs(teleports) do
  36.         displayLine()
  37.         monit.write(k .. " " .. v)
  38.         dispNewLine()
  39.         displayLine()
  40.     end
  41. end
  42.  
  43.  
  44. function work()
  45.     while true do
  46.         local event, side, x, y = os.pullEvent("monitor_touch")
  47.         local btnClicked = math.ceil(y / 3)
  48.         print(event.."; x="..x.."; y="..y)
  49.         print("Button clicked = "..btnClicked)
  50.         if btnClicked <= #teleports and btnClicked > 0 then
  51.             print(teleports[btnClicked][1] .. " -> " .. teleports[btnClicked][2])
  52.             local hostIndex = math.ceil(btnClicked / 5)
  53.             local targetHostID = hostIDs[hostIndex]
  54.             print("try to send side to host")
  55.             local result = rednet.send(targetHostID, teleports[btnClicked][1], "teleportSystem1")
  56.             print("Is it successful? -> "..tostring(result))
  57.         else
  58.             print("This is not button")
  59.         end
  60.         print("end of operation")
  61.         print("")
  62.     end
  63. end
  64.  
  65.  
  66. startOperations()
  67. display()
  68. work()
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement