Advertisement
Sebaxus

PathFinder - Node (augment handler)

Sep 28th, 2020 (edited)
1,526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.47 KB | None | 0 0
  1. component = require("component")
  2. computer = require("computer")
  3. event = require("event")
  4.  
  5. --NETWORK
  6. port = 1
  7. C = component.modem
  8. C.setWakeMessage("BOOT") --Boots the machine through networking
  9.  
  10. --REDSTONE
  11. cableSide = 3 --Side where bundled cable is attached
  12. R = component.redstone
  13.  
  14.  
  15. if C.open(port) then
  16.     print("Port "..port.." is open!")
  17. else
  18.     computer.shutdown(true)
  19. end
  20.  
  21. --Add UUID's from file if the file exists
  22. UUids = {}
  23. file = io.open("uuids.txt", "r")
  24. if file ~= nil then
  25.     repeat
  26.         line = file:read("*l")
  27.         if line ~= nil then
  28.             a, b = string.find(line, "/")
  29.             name = string.sub(line, 1, a-1)
  30.             uuid = string.sub(line, b+1)
  31.             if component.get(uuid) ~= nil then
  32.                 UUids[uuid] = name
  33.             end
  34.         end
  35.     until line == nil
  36.     io.close(file)
  37. end
  38.  
  39. --Check currently attached
  40. updateNeeded = false
  41. Attached = component.list("ir_augment_detector")
  42. for address, componentType in component.list("ir_augment_detector") do
  43.     if UUids[address] == nil then
  44.         updateNeeded = true
  45.         selected = component.proxy(address)
  46.         print("UUID: "..address.." Not yet registered!")
  47.         x, y, z = selected.getPos()
  48.         print("Detector at [X: "..x..", Y: "..y..", Z: "..z.."]")
  49.         print("Enter point name: ")
  50.         UUids[address] = io.read()
  51.     end
  52. end
  53.  
  54. --Write UUIDS back
  55. if updateNeeded then
  56.     file = io.open("uuids.txt", "r")
  57.     if file ~= nil then
  58.         os.execute("rm uuids.txt")
  59.     end
  60.     io.close(file)
  61.    
  62.     file = io.open("uuids.txt", "a")
  63.     if file ~= nil then
  64.         print("Writing UUIDS to file!")
  65.         for address, componentType in component.list("ir_augment_detector") do
  66.             name = UUids[address]
  67.             local toWrite = name.."/"..address.."\n"
  68.             file:write(toWrite)
  69.             file:flush()
  70.         end
  71.     end
  72.     io.close(file)
  73. end
  74.  
  75. print("Done Verifying UUIDS!! ;)")
  76.  
  77. --Check for track situations from file (Switches, Xpoints, Etc..)
  78. Switches = {}
  79. Xpoints = {}
  80.  
  81. file = io.open("track.txt", "r")
  82. if file ~= nil then
  83.     repeat
  84.         line = file:read("*l")
  85.         if line ~= nil then
  86.             local a, b = string.find(line, "/")
  87.             local name = string.sub(line, 1, a-1)
  88.             if string.find(name, "S") ~= nil then
  89.                 local number = string.sub(line, 2, a-1)
  90.                 local color = tonumber(string.sub(line, b+1))
  91.                
  92.                 Switches[number] = color
  93.             elseif string.find(name, "X") ~= nil then
  94.                 local number = string.sub(line, 2, a-1)
  95.                 local secondSlash = string.sub(line, b+1)
  96.                 local c, d = string.find(secondSlash, "/")
  97.                 local colorA = tonumber(string.sub(secondSlash, 1, c-1))
  98.                 local colorB = tonumber(string.sub(secondSlash, d+1))
  99.                
  100.                 Xpoints[number] = {}
  101.                 Xpoints[number][0] = colorA
  102.                 Xpoints[number][1] = colorB
  103.             end
  104.         end
  105.     until line == nil
  106. end
  107.  
  108. --Check for currently registered track
  109. updateNeeded = false
  110. for address, componentType in component.list("ir_augment_detector") do
  111.     name = UUids[address]
  112.     --S4i0 X3i2
  113.     local a, b = string.find(name, "i")
  114.     if string.find(name, "S") ~= nil then
  115.         local number = string.sub(name, 2, a-1)
  116.         if Switches[number] == nil then
  117.             updateNeeded = true
  118.             print("Track Piece: "..name.." wasn't added to a redstone output")
  119.             print("Please enter output color number")
  120.             Switches[number] = tonumber(io.read())
  121.         end
  122.     elseif string.find(name, "X") ~= nil then
  123.         local number = string.sub(name, 2, a-1)
  124.         if Xpoints[number] == nil then
  125.             updateNeeded = true
  126.             Xpoints[number] = {}
  127.             print("Track Piece: "..name.." wasn't added to a redstone output")
  128.             print("Please enter output colorA number (i0 > i3)")
  129.             Xpoints[number][0] = tonumber(io.read())
  130.             print("Please enter output colorB number (i1 > i2)")
  131.             Xpoints[number][1] = tonumber(io.read())
  132.         end
  133.     end
  134. end
  135.  
  136. --Updated registered track pieces if needed
  137. if updateNeeded then
  138.     file = io.open("track.txt", "r")
  139.     if file ~= nil then
  140.         os.execute("rm track.txt")
  141.     end
  142.     io.close(file)
  143.     file = io.open("track.txt", "a")
  144.     if file ~= nil then
  145.         for trackNumber, Output in pairs(Switches) do
  146.             local toWrite = "S"..trackNumber.."/"..Output.."\n"
  147.             file:write(toWrite)
  148.             file:flush()
  149.         end
  150.        
  151.         for trackNumber, Outputs in pairs(Xpoints) do
  152.             local toWrite = "X"..trackNumber.."/"..Outputs[0].."/"..Outputs[1].."\n"
  153.             file:write(toWrite)
  154.             file:flush()
  155.         end
  156.         io.close(file)
  157.     end
  158. end
  159.  
  160. print("Done verifying track pieces!! ;)")
  161.  
  162. function setR(color, state)
  163.   color = tonumber(color)
  164.   if state then state = 255 else state = 0 end
  165.   R.setBundledOutput(cableSide, color, state)
  166. end
  167.  
  168. function setSwitch(arg)
  169.   --get input and ouput and switch the Switch accordingly
  170.   local a, b = string.find(arg, "i")
  171.   local c, d = string.find(arg, "o")
  172.   number = string.sub(arg, 3, a-1)
  173.   input = tonumber(string.sub(arg, b+1, c-1))
  174.   output = tonumber(string.sub(arg, d+1))
  175.  
  176.   if input == 1 and output == 0 then
  177.     setR(Switches[number], false)
  178.   elseif input == 2 and output == 0 then
  179.     setR(Switches[number], true)
  180.   elseif input == 0 and output == 1 then
  181.     setR(Switches[number], false)
  182.   elseif input == 0 and output == 2 then
  183.     setR(Switches[number], true)
  184.   end
  185. end
  186.  
  187. function setXpoint(arg)
  188.   --get input and ouput and switch the Xpoint accordingly
  189.   local a, b = string.find(arg, "i")
  190.   local c, d = string.find(arg, "o")
  191.   number = string.sub(arg, 3, a-1)
  192.   input = tonumber(string.sub(arg, b+1, c-1))
  193.   output = tonumber(string.sub(arg, d+1))
  194.  
  195.   if input == 0 and output == 2 then
  196.     setR(Xpoints[number][0], false)
  197.     setR(Xpoints[number][1], false)
  198.   elseif input == 2 and output == 0 then
  199.     setR(Xpoints[number][0], false)
  200.     setR(Xpoints[number][1], false)
  201.   elseif input == 1 and output == 2 then
  202.     setR(Xpoints[number][0], false)
  203.     setR(Xpoints[number][1], true)
  204.   elseif input == 2 and output == 1 then
  205.     setR(Xpoints[number][0], false)
  206.     setR(Xpoints[number][1], true)
  207.   elseif input == 1 and output == 3 then
  208.     setR(Xpoints[number][0], false)
  209.     setR(Xpoints[number][1], false)
  210.   elseif input == 3 and output == 1 then
  211.     setR(Xpoints[number][0], false)
  212.     setR(Xpoints[number][1], false)
  213.   elseif input == 0 and output == 3 then
  214.     setR(Xpoints[number][0], true)
  215.     setR(Xpoints[number][1], false)
  216.   elseif input == 3 and output == 0 then
  217.     setR(Xpoints[number][0], true)
  218.     setR(Xpoints[number][1], false)
  219.   end
  220. end
  221.  
  222. --PROGRAM
  223. last = {}
  224. last[0] = 0
  225. last[1] = 0
  226. i = 0
  227.  
  228. while true do
  229.   nameEvent, id, from, port, _, message = event.pullMultiple("ir_train_overhead", "modem_message")
  230.  
  231.   if nameEvent == "ir_train_overhead" then
  232.     detector = component.proxy(id)
  233.     detector.setTag("1")
  234.     info = detector.info()
  235.     if info ~= nil and id ~= nil and UUids[id] ~= last[0] and UUids[id] ~= last[1] then
  236.       message = UUids[id].."T"..info.tag
  237.       print(message)
  238.       C.broadcast(1, message)
  239.       last[i] = UUids[id]
  240.       if i == 1 then
  241.         i = 0
  242.       else
  243.         i = 1
  244.       end
  245.     end
  246.   elseif nameEvent == "modem_message" then
  247.     if message ~= lastmessage and message ~= C.getWakeMessage() then
  248.       print("[R] "..message)
  249.       lastmessage = message
  250.  
  251.       --Redstone Controlled by Management
  252.       if string.find(message, "RX") ~= nil
  253.       then
  254.         local a, b = string.find(message, "i")
  255.         number = string.sub(message, 3, a-1)
  256.          if Xpoints[number] ~= nil
  257.         then
  258.           setXpoint(message)
  259.         end
  260.       elseif string.find(message, "RS") ~= nil
  261.       then
  262.         local a, b = string.find(message, "i")
  263.         number = string.sub(message, 3, a-1)
  264.         if Switches[number] ~= nil
  265.         then
  266.           setSwitch(message)
  267.         end
  268.       end
  269.     end
  270.   end
  271. end
  272.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement