Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- component = require("component")
- computer = require("computer")
- event = require("event")
- --NETWORK
- port = 1
- C = component.modem
- C.setWakeMessage("BOOT") --Boots the machine through networking
- --REDSTONE
- cableSide = 3 --Side where bundled cable is attached
- R = component.redstone
- if C.open(port) then
- print("Port "..port.." is open!")
- else
- computer.shutdown(true)
- end
- --Add UUID's from file if the file exists
- UUids = {}
- file = io.open("uuids.txt", "r")
- if file ~= nil then
- repeat
- line = file:read("*l")
- if line ~= nil then
- a, b = string.find(line, "/")
- name = string.sub(line, 1, a-1)
- uuid = string.sub(line, b+1)
- if component.get(uuid) ~= nil then
- UUids[uuid] = name
- end
- end
- until line == nil
- io.close(file)
- end
- --Check currently attached
- updateNeeded = false
- Attached = component.list("ir_augment_detector")
- for address, componentType in component.list("ir_augment_detector") do
- if UUids[address] == nil then
- updateNeeded = true
- selected = component.proxy(address)
- print("UUID: "..address.." Not yet registered!")
- x, y, z = selected.getPos()
- print("Detector at [X: "..x..", Y: "..y..", Z: "..z.."]")
- print("Enter point name: ")
- UUids[address] = io.read()
- end
- end
- --Write UUIDS back
- if updateNeeded then
- file = io.open("uuids.txt", "r")
- if file ~= nil then
- os.execute("rm uuids.txt")
- end
- io.close(file)
- file = io.open("uuids.txt", "a")
- if file ~= nil then
- print("Writing UUIDS to file!")
- for address, componentType in component.list("ir_augment_detector") do
- name = UUids[address]
- local toWrite = name.."/"..address.."\n"
- file:write(toWrite)
- file:flush()
- end
- end
- io.close(file)
- end
- print("Done Verifying UUIDS!! ;)")
- --Check for track situations from file (Switches, Xpoints, Etc..)
- Switches = {}
- Xpoints = {}
- file = io.open("track.txt", "r")
- if file ~= nil then
- repeat
- line = file:read("*l")
- if line ~= nil then
- local a, b = string.find(line, "/")
- local name = string.sub(line, 1, a-1)
- if string.find(name, "S") ~= nil then
- local number = string.sub(line, 2, a-1)
- local color = tonumber(string.sub(line, b+1))
- Switches[number] = color
- elseif string.find(name, "X") ~= nil then
- local number = string.sub(line, 2, a-1)
- local secondSlash = string.sub(line, b+1)
- local c, d = string.find(secondSlash, "/")
- local colorA = tonumber(string.sub(secondSlash, 1, c-1))
- local colorB = tonumber(string.sub(secondSlash, d+1))
- Xpoints[number] = {}
- Xpoints[number][0] = colorA
- Xpoints[number][1] = colorB
- end
- end
- until line == nil
- end
- --Check for currently registered track
- updateNeeded = false
- for address, componentType in component.list("ir_augment_detector") do
- name = UUids[address]
- --S4i0 X3i2
- local a, b = string.find(name, "i")
- if string.find(name, "S") ~= nil then
- local number = string.sub(name, 2, a-1)
- if Switches[number] == nil then
- updateNeeded = true
- print("Track Piece: "..name.." wasn't added to a redstone output")
- print("Please enter output color number")
- Switches[number] = tonumber(io.read())
- end
- elseif string.find(name, "X") ~= nil then
- local number = string.sub(name, 2, a-1)
- if Xpoints[number] == nil then
- updateNeeded = true
- Xpoints[number] = {}
- print("Track Piece: "..name.." wasn't added to a redstone output")
- print("Please enter output colorA number (i0 > i3)")
- Xpoints[number][0] = tonumber(io.read())
- print("Please enter output colorB number (i1 > i2)")
- Xpoints[number][1] = tonumber(io.read())
- end
- end
- end
- --Updated registered track pieces if needed
- if updateNeeded then
- file = io.open("track.txt", "r")
- if file ~= nil then
- os.execute("rm track.txt")
- end
- io.close(file)
- file = io.open("track.txt", "a")
- if file ~= nil then
- for trackNumber, Output in pairs(Switches) do
- local toWrite = "S"..trackNumber.."/"..Output.."\n"
- file:write(toWrite)
- file:flush()
- end
- for trackNumber, Outputs in pairs(Xpoints) do
- local toWrite = "X"..trackNumber.."/"..Outputs[0].."/"..Outputs[1].."\n"
- file:write(toWrite)
- file:flush()
- end
- io.close(file)
- end
- end
- print("Done verifying track pieces!! ;)")
- function setR(color, state)
- color = tonumber(color)
- if state then state = 255 else state = 0 end
- R.setBundledOutput(cableSide, color, state)
- end
- function setSwitch(arg)
- --get input and ouput and switch the Switch accordingly
- local a, b = string.find(arg, "i")
- local c, d = string.find(arg, "o")
- number = string.sub(arg, 3, a-1)
- input = tonumber(string.sub(arg, b+1, c-1))
- output = tonumber(string.sub(arg, d+1))
- if input == 1 and output == 0 then
- setR(Switches[number], false)
- elseif input == 2 and output == 0 then
- setR(Switches[number], true)
- elseif input == 0 and output == 1 then
- setR(Switches[number], false)
- elseif input == 0 and output == 2 then
- setR(Switches[number], true)
- end
- end
- function setXpoint(arg)
- --get input and ouput and switch the Xpoint accordingly
- local a, b = string.find(arg, "i")
- local c, d = string.find(arg, "o")
- number = string.sub(arg, 3, a-1)
- input = tonumber(string.sub(arg, b+1, c-1))
- output = tonumber(string.sub(arg, d+1))
- if input == 0 and output == 2 then
- setR(Xpoints[number][0], false)
- setR(Xpoints[number][1], false)
- elseif input == 2 and output == 0 then
- setR(Xpoints[number][0], false)
- setR(Xpoints[number][1], false)
- elseif input == 1 and output == 2 then
- setR(Xpoints[number][0], false)
- setR(Xpoints[number][1], true)
- elseif input == 2 and output == 1 then
- setR(Xpoints[number][0], false)
- setR(Xpoints[number][1], true)
- elseif input == 1 and output == 3 then
- setR(Xpoints[number][0], false)
- setR(Xpoints[number][1], false)
- elseif input == 3 and output == 1 then
- setR(Xpoints[number][0], false)
- setR(Xpoints[number][1], false)
- elseif input == 0 and output == 3 then
- setR(Xpoints[number][0], true)
- setR(Xpoints[number][1], false)
- elseif input == 3 and output == 0 then
- setR(Xpoints[number][0], true)
- setR(Xpoints[number][1], false)
- end
- end
- --PROGRAM
- last = {}
- last[0] = 0
- last[1] = 0
- i = 0
- while true do
- nameEvent, id, from, port, _, message = event.pullMultiple("ir_train_overhead", "modem_message")
- if nameEvent == "ir_train_overhead" then
- detector = component.proxy(id)
- detector.setTag("1")
- info = detector.info()
- if info ~= nil and id ~= nil and UUids[id] ~= last[0] and UUids[id] ~= last[1] then
- message = UUids[id].."T"..info.tag
- print(message)
- C.broadcast(1, message)
- last[i] = UUids[id]
- if i == 1 then
- i = 0
- else
- i = 1
- end
- end
- elseif nameEvent == "modem_message" then
- if message ~= lastmessage and message ~= C.getWakeMessage() then
- print("[R] "..message)
- lastmessage = message
- --Redstone Controlled by Management
- if string.find(message, "RX") ~= nil
- then
- local a, b = string.find(message, "i")
- number = string.sub(message, 3, a-1)
- if Xpoints[number] ~= nil
- then
- setXpoint(message)
- end
- elseif string.find(message, "RS") ~= nil
- then
- local a, b = string.find(message, "i")
- number = string.sub(message, 3, a-1)
- if Switches[number] ~= nil
- then
- setSwitch(message)
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement