Advertisement
Nemirel345

telepad.lua

Mar 24th, 2023 (edited)
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. chanel = 104 --Chanel of this teleporter
  2.  
  3. chest = peripheral.wrap("bottom")
  4. modem = peripheral.wrap("right")
  5. modem.open(chanel)
  6.  
  7. target = {
  8.     chanel = nil,
  9.     timeout = 0,
  10.     locked = false
  11. }
  12. targetTimeoutDelay = 100
  13. targets = {
  14.     {101,"test 1"},
  15.     {102,"test 2"},
  16.     {103,"test 3"}
  17. }
  18.  
  19. monitors = {}
  20. scale = 0.5
  21.  
  22.  
  23. function redstone.pulse(side, delay, pulse)
  24.     delay, pulse = delay or 0.1, pulse or true
  25.     redstone.setOutput(side, pulse)
  26.     sleep(delay)
  27.     redstone.setOutput(side, not pulse)
  28. end
  29.  
  30. function activate()
  31.     print("Pushing Disk")
  32.     chest.pushItems("top",1)
  33.     sleep(1)
  34.     print("Activating Spacial IO")
  35.     redstone.pulse("back",0.2,true)
  36.     waitForDisk()
  37.     print("Done")
  38.     drawScreens()
  39. end
  40.  
  41. function waitForDisk()
  42.     print("Waiting for Disk")
  43.     while not chest.getItemDetail(1) do
  44.         sleep(0.2)
  45.     end
  46.     print("Disk Recieved")
  47. end
  48.  
  49. function touchscreen(x,y)
  50.  
  51. end
  52.  
  53. function selectTarget(x,y)
  54.     for i,v in ipairs(targets) do
  55.         if y == i+1 then
  56.             return v[1]
  57.         end
  58.     end
  59.     return nil
  60. end
  61.  
  62. function requestTarget(n)
  63.     target.chanel = n
  64.     target.timeout = targetTimeoutDelay
  65.     print("Pinging target")
  66.     modem.transmit(target.chanel,chanel,"PING")
  67. end
  68.  
  69. function timeoutTarget()
  70.     target.timeout = 0
  71.     target.chanel = nil
  72.     target.locked = false
  73. end
  74.  
  75. function handleMessage(message, reply)
  76.     if message == "activate" then
  77.         print("Remote activation requested")
  78.         activate()
  79.         modem.transmit(reply,chanel,"activated")
  80.         print("Confirmation sent")
  81.     elseif message == "activated" then
  82.         print("Confirmation recieved")
  83.         print("Transfering target here")
  84.         activate()
  85.         target.chanel = nil
  86.     elseif message == "PING" then
  87.         print("Remote request incoming, answering")
  88.         modem.transmit(reply,chanel,"PONG")
  89.     elseif message == "PONG" then
  90.         print("Target answered")
  91.         if reply == target.chanel then
  92.             target.locked = true
  93.             target.timeout = 0
  94.             activate()
  95.             sendTarget()
  96.         end
  97.     end
  98. end
  99.  
  100. function sendTarget()
  101.     print("Sending remote activation request")
  102.     modem.transmit(target.chanel,chanel,"activate")
  103.     target.locked = false
  104.     target.timeout = targetTimeoutDelay
  105. end
  106.  
  107. function writeText(monitor, text,y,align,margin,padding)
  108.     text = text or ""
  109.     y = y or 1
  110.     align = align or "left"
  111.     margin = margin or 0
  112.     padding = padding or 0
  113.     local x = margin
  114.     if align == "center" then
  115.         --text = (string.len(text)%2 == 0) and text or text.." "
  116.         text = padText(text,padding,padding," ")
  117.         x = monitor.size.x/2-(string.len(text)/2-1)
  118.     elseif align == "right" then
  119.         text = padText(text,0,padding," ")
  120.         x = monitor.size.x-(string.len(text)-1)-margin
  121.     else
  122.         text = padText(text,padding,0," ")
  123.     end
  124.     monitor.setCursorPos(x,y)
  125.     monitor.write(text)
  126. end
  127.  
  128. function padText(text,left,right,char)
  129.     left,right,char = left or 0,right or 0,char or " "
  130.     return string.rep(char,left)..text..string.rep(char,right)
  131. end
  132.  
  133. function setColors(monitor, fg, bg)
  134.     fg,bg = fg or colors.white,bg or colors.black
  135.     monitor.setTextColor(fg)
  136.     monitor.setBackgroundColour(bg)
  137. end
  138.  
  139. function setupMonitors()
  140.     print("Setting up Monitors")
  141.     monitors = {}
  142.     for i,v in ipairs(peripheral.getNames()) do
  143.         if string.sub(v,1,7) == "monitor" then
  144.             local monitor = peripheral.wrap(v)
  145.             setColors(monitor)
  146.             monitor.clear()
  147.             monitor.setCursorBlink(false)
  148.             monitor.setTextScale(scale)
  149.  
  150.             table.insert(monitors,monitor)
  151.  
  152.             monitor.size = {}
  153.             monitor.size.x,
  154.              monitor.size.y = monitor.getSize()
  155.             print(monitor.size.x,monitor.size.y)
  156.         end
  157.     end
  158.     drawScreens()
  159. end
  160.  
  161. function drawScreens()
  162.     for i,monitor in ipairs(monitors) do
  163.         drawScreen(monitor)
  164.     end
  165. end
  166.  
  167. function drawScreen(monitor)
  168.     if target.chanel then
  169.         setColors(monitor)
  170.         monitor.clear()
  171.     elseif target.locked then
  172.         setColors(monitor)
  173.         monitor.clear()
  174.     else
  175.         setColors(monitor,colors.red,colors.gray)
  176.         writeText(monitor, "Destination",1,"center",0,1)
  177.     end
  178. end
  179.  
  180. do --First Time Setup
  181.     setupMonitors()
  182. end
  183.  
  184. while true do
  185.     local eventData = {os.pullEvent()}
  186.     local event = eventData[1]
  187.     if event ~= "key" and event ~= "key_up" then
  188.         print(table.unpack(eventData))
  189.     end
  190.  
  191.     if event == "modem_message" then
  192.         local event, side, channel, reply, message, distance = table.unpack(eventData)
  193.         print(message)
  194.         handleMessage(message, reply)
  195.     elseif target.chanel and not target.locked then
  196.         target.timeout = target.timeout-1
  197.         if target.timeout <= 0 then
  198.             timeoutTarget()
  199.         end
  200.     elseif event == "monitor_touch" then
  201.         activate()
  202.     elseif event == "peripheral" or
  203.      event == "peripheral_detach" or
  204.      event == "monitor_resize" then
  205.         setupMonitors()
  206.     end
  207. end
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement