Krobys

[Computer] Teleport Computer Part

Mar 18th, 2023 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.69 KB | Gaming | 0 0
  1. local component = require("component")
  2. local GUI = require("GUI")
  3. local chest = component.diamond or component.chest
  4. local chestSize = chest.getInventorySize()
  5. local modem = component.modem
  6. local serialization = require("serialization")
  7. local computer = require("computer")
  8. local text = require("text")
  9. local event = require("event")
  10. local broadcastPort = 51
  11. local BROADCAST_TYPE = "REQUEST_TELEPORT"
  12.  
  13. local zeroPointName = "ZERO_POINT"
  14.  
  15. local leftBorder = 2
  16. local topBorder = 6
  17.  
  18. local application = GUI.application()
  19. local buttonsInLine = 5
  20. local buttonsGap = 1
  21. local buttonWidth = math.floor((application.width / buttonsInLine) - (buttonsGap * 2))
  22. local buttonHeight = 1
  23.  
  24. application:addChild(GUI.panel(1, 1, application.width, application.height, 0x262626))
  25. local containerButtons = application:addChild(GUI.container(1, 1, application.width, application.height))
  26. local topInfoText = application:addChild(GUI.text(2, 2, 0xFFFFFF, ""))
  27. topInfoText.hidden = true
  28. local infoText = application:addChild(GUI.text(2, 3, 0xFFFFFF, " "))
  29. infoText.hidden = true
  30.  
  31. local buttons = {}
  32.  
  33. local function onFilterByInput(input)
  34.     local trimmedInput = text.trim(input)
  35.     for ind = 1, #buttons do
  36.         local button = buttons[ind]
  37.         if trimmedInput == "" or string.find(button.text, trimmedInput) then
  38.             button.hidden = false
  39.         else
  40.             button.hidden = true
  41.         end
  42.     end
  43.     application:draw()
  44. end
  45.  
  46. local input = application:addChild(GUI.input(leftBorder + (buttonWidth + buttonsGap) * 3, 2, buttonWidth, 3, 0xEEEEEE, 0x555555, 0x999999, 0xFFFFFF, 0x2D2D2D, "", "Фильтр точек"))
  47. input.onInputFinished = function()
  48.     onFilterByInput(input.text)
  49. end
  50.  
  51. local function sendRequestToRobotForTeleport(teleportItem)
  52.     local requestSerialised = serialization.serialize({type = BROADCAST_TYPE, payload = teleportItem})
  53.     modem.broadcast(broadcastPort, requestSerialised)
  54. end
  55.  
  56. local function requestTeleport(teleportItem)
  57.     local isZeroPointPresent = false
  58.     for ind = 1, chestSize do
  59.         if chest.getStackInSlot(ind) then
  60.             if chest.getStackInSlot(ind).display_name == zeroPointName then
  61.                 isZeroPointPresent = true
  62.                 break
  63.             end
  64.         end
  65.     end
  66.     if isZeroPointPresent then
  67.         infoText.text = "Запрошен телепорт в ".."["..teleportItem.display_name.."]"
  68.         infoText.hidden = false
  69.         application:draw()
  70.         sendRequestToRobotForTeleport(teleportItem)
  71.         computer.pullSignal(3)
  72.         infoText.hidden = true
  73.         topInfoText.text = "Последний телепорт: "..teleportItem.display_name
  74.         topInfoText.hidden = false
  75.         application:draw()
  76.     else
  77.         infoText.text = "ДРУГОЙ ТЕЛЕПОРТ УЖЕ В ПРОЦЕССЕ"
  78.         infoText.hidden = false
  79.         application:draw()
  80.         computer.pullSignal(3)
  81.         infoText.hidden = true
  82.         application:draw()
  83.     end
  84. end
  85.  
  86. local function checkCoordinatesInChest()
  87.     containerButtons:removeChildren()
  88.     buttons = {}
  89.     local currentX = leftBorder
  90.     local currentY = topBorder
  91.     local ind = 1
  92.     for index = 1, chestSize do
  93.         local itemInChest = chest.getStackInSlot(index)
  94.         if itemInChest and itemInChest.display_name ~= zeroPointName then
  95.             local button = containerButtons:addChild(GUI.button(currentX, currentY, buttonWidth, buttonHeight, 0xFFFFFF, 0x000000, 0xCCCCCC, 0xFFFFFF, itemInChest.display_name))
  96.             button.animated = false
  97.             button.onTouch = function()
  98.                 if itemInChest then
  99.                     requestTeleport(itemInChest)
  100.                 end
  101.             end
  102.             table.insert(buttons, button)
  103.             currentX = currentX + buttonWidth + buttonsGap
  104.             if (ind % buttonsInLine) == 0 then -- start a new line after buttonsInLine items
  105.                 currentX = leftBorder
  106.                 currentY = currentY + buttonHeight + buttonsGap
  107.             end
  108.             ind = ind + 1
  109.         end
  110.     end
  111. end
  112.  
  113. local function refresh()
  114.     checkCoordinatesInChest()
  115.     infoText.text = "Список обновлён!"
  116.     infoText.hidden = false
  117.     application:draw()
  118.     computer.pullSignal(3)
  119.     infoText.hidden = true
  120.     application:draw()
  121. end
  122.  
  123. function event.shouldInterrupt()
  124.     return false
  125. end
  126.  
  127. local refreshButton = application:addChild(GUI.button(leftBorder + (buttonWidth + buttonsGap) * 2, 2, buttonWidth, 3, 0xFFFFFF, 0x000000, 0xCCCCCC, 0xFFFFFF, "Обновить список"))
  128. refreshButton.animated = false
  129. refreshButton.onTouch = function()
  130.     refresh()
  131. end
  132.  
  133. checkCoordinatesInChest()
  134.  
  135. application:draw()
  136. application:start()
Advertisement
Add Comment
Please, Sign In to add comment