abstract_abstract

teleport.lua

Nov 4th, 2024 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.54 KB | None | 0 0
  1. local teleports = {}
  2. local textScale = 0.9
  3. rednet.open("left")
  4.  
  5.  
  6. local function initTeleports()
  7.     print("Start startOperations")
  8.  
  9.     teleports = {}
  10.     local desTP = {}
  11.     local computerID = os.getComputerID()
  12.     rednet.send(126, "", "dns_list")
  13.    
  14.     local senderID, message, protocol = rednet.receive("dns_list_response", 5)
  15.  
  16.     if senderID then
  17.         desTP = textutils.unserialize(message)      
  18.         local file = fs.open("dns_table.txt", 'w')
  19.         if file then
  20.             file.write(textutils.serialize(desTP))
  21.             file.close()
  22.         end
  23.     else
  24.         print("DNS server is not reachable.")
  25.         local file = fs.open("dns_table.txt", 'r')
  26.         if file then
  27.             desTP = textutils.unserialize(file.readAll())
  28.             file.close()
  29.         end
  30.     end
  31.  
  32.     local index = 1        
  33.     for id, name in pairs(desTP) do
  34.         if not (computerID == id) then
  35.             teleports[index] = {id = id, name = name}
  36.             index = index + 1
  37.         end
  38.     end
  39.     print("End startOperations")
  40. end
  41.  
  42.  
  43. local function initialDisplay()
  44.     print("Start initialDisplay()")
  45.     local monitor = peripheral.wrap("top")
  46.     monitor.setTextScale(textScale)
  47.     monitor.setBackgroundColour(colors.blue)
  48.     monitor.clear()
  49.     monitor.setCursorPos(1, 1)
  50.  
  51.     local function dispNewLine()
  52.         local x, y = monitor.getCursorPos()
  53.         monitor.setCursorPos(1, y + 1)
  54.     end
  55.  
  56.     local function displayLine()
  57.         local width = monitor.getSize()
  58.         for i = 1, width do
  59.             monitor.write("-")
  60.         end
  61.         dispNewLine()
  62.     end
  63.  
  64.     for k, v in ipairs(teleports) do
  65.         displayLine()
  66.         monitor.write(k .. " " .. v.name)
  67.         dispNewLine()
  68.         displayLine()
  69.     end
  70.     print("End initialDisplay()")
  71. end
  72.  
  73.  
  74. local function clickPocketDimension()
  75.     print("Start clickPocketDimension()")
  76.     redstone.setOutput("bottom", true)
  77.     os.sleep(1)
  78.     redstone.setOutput("bottom", false)
  79.     os.sleep(1)
  80.     print("End clickPocketDimension()")    
  81. end
  82.  
  83.  
  84. local function dcPD()
  85.     print("Start dcPD()")
  86.     clickPocketDimension()
  87.     clickPocketDimension()
  88.     print("End dcPD()")
  89. end
  90.  
  91.  
  92. local function sendLog(destination)
  93.     print("Start sendLog")
  94.     rednet.send(126, destination, "log")
  95.     local senderID, message, protocol = rednet.receive("dns_list_response", 5)
  96.     if senderID then
  97.         print(message)
  98.     end
  99.     print("End sendLog")
  100. end
  101.  
  102.  
  103. local function clickDisplay()
  104.     while true do
  105.         print("Start clickDisplay()")
  106.         ::continue::
  107.  
  108.         local event, side, x, y = os.pullEvent("monitor_touch")
  109.         local btnClicked = math.ceil(y / 3)
  110.          
  111.         if btnClicked > #teleports and btnClicked > 0 then
  112.             initialDisplay()
  113.             goto continue
  114.         end
  115.         dcPD()
  116.         sendLog(teleports[btnClicked])
  117.    
  118.         rednet.send(teleports[btnClicked].id, "Sended", "message")
  119.         initialDisplay()
  120.         print("End clickDisplay()")
  121.     end
  122. end
  123.  
  124.  
  125. local function runRednet()
  126.     while true do
  127.         print("Start runRednet()")
  128.         initialDisplay()
  129.         local senderID, message, protocol = rednet.receive()
  130.    
  131.         if protocol == "message" then
  132.             sleep(5)
  133.             dcPD()
  134.         end
  135.         print("End runRednet()")
  136.     end
  137. end
  138.  
  139.  
  140. local function main()
  141.     print("Start main()")
  142.     sleep(2)
  143.     initTeleports()
  144.     initialDisplay()
  145.     parallel.waitForAll(clickDisplay, runRednet)    
  146.     print("End main()")
  147. end
  148.  
  149. main()
  150.  
Advertisement
Add Comment
Please, Sign In to add comment