Krobys

[Robot] Teleport Robot Part

Mar 18th, 2023 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | Gaming | 0 0
  1. local component = require("component")
  2. local robot = require("robot")
  3. local redstone = component.redstone
  4. local inventoryController = component.inventory_controller
  5. local modem = component.modem
  6. local event = require("event")
  7. local sides = require("sides")
  8. local serialization = require("serialization")
  9. local isInProgress = false
  10. local broadcastPort = 51
  11. local BROADCAST_TYPE = "REQUEST_TELEPORT"
  12.  
  13. modem.open(broadcastPort)
  14. robot.select(1)
  15.  
  16. local zeroCardName = "ZERO_POINT"
  17. local function launchWithCard(teleportItemCard)
  18.     robot.select(1)
  19.     local tpCardTook = false
  20.     local zeroCardTook = false
  21.  
  22.     local inventorySize = inventoryController.getInventorySize(sides.bottom)
  23.     for index = 1, inventorySize do
  24.         --take zero card
  25.         --take normal card
  26.         local item = inventoryController.getStackInSlot(sides.bottom, index)
  27.         if item and item.label == zeroCardName then
  28.             print("Took zero card")
  29.             inventoryController.suckFromSlot(sides.bottom, index)
  30.             zeroCardTook = true
  31.         end
  32.         if item and item.label == teleportItemCard.display_name then
  33.             print("Took tp card")
  34.             inventoryController.suckFromSlot(sides.bottom, index)
  35.             tpCardTook = true
  36.         end
  37.         if zeroCardTook and tpCardTook then
  38.             break
  39.         end
  40.     end
  41.     for innInd = 1, robot.inventorySize() do
  42.         --setup with zero card
  43.         local item = inventoryController.getStackInInternalSlot(innInd)
  44.         if item and item.label == zeroCardName then
  45.             print("Found zero card inner")
  46.             robot.select(innInd)
  47.             inventoryController.equip()
  48.             robot.use(sides.front, true)
  49.             print("Used zero card inner")
  50.             break
  51.         end
  52.     end
  53.     --activate_redstone
  54.     print("Set redstone output")
  55.     redstone.setOutput(sides.front, 15)
  56.     os.sleep(0.01)
  57.     redstone.setOutput(sides.front, 0)
  58.     for innInd = 1, robot.inventorySize() do
  59.         --setup with zero card
  60.         local item = inventoryController.getStackInInternalSlot(innInd)
  61.         if item and item.label == teleportItemCard.display_name then
  62.             robot.select(innInd)
  63.             inventoryController.equip()
  64.             robot.use(sides.front, true)
  65.             print("Used actual card")
  66.  
  67.             --back cards to chest
  68.             robot.dropDown()
  69.             print("Back zero card to chest")
  70.             inventoryController.equip()
  71.             robot.dropDown()
  72.             print("Back actual card to chest")
  73.             break
  74.         end
  75.     end
  76. end
  77.  
  78. local function handleBroadcast(_, _, _, port, _, message)
  79.     if message then
  80.         local messageUnserialized = serialization.unserialize(message)
  81.         if port == broadcastPort and messageUnserialized.type == BROADCAST_TYPE then
  82.             if not isInProgress then
  83.                 isInProgress = true
  84.                 print("Запрос на телепорт в "..messageUnserialized.payload.display_name.. " получен")
  85.                 launchWithCard(messageUnserialized.payload)
  86.                 isInProgress = false
  87.             end
  88.         end
  89.     end
  90. end
  91.  
  92. event.listen("modem_message", handleBroadcast)
  93.  
  94. while true do
  95.     event.pull()
  96. end
Advertisement
Add Comment
Please, Sign In to add comment