Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- === COMPONENT SETUP ===
- local component = require("component")
- local traffic = component.traffic_light_card
- local os = require("os")
- local thread = require("thread")
- local sides = require("sides")
- local colors = require("colors")
- local rs = component.redstone
- -- === BULB DEFINITIONS ===
- BULBS = {
- RED = "Red", YELLOW = "Yellow", GREEN = "Green",
- RED_LEFT = "RedArrowLeft", YELLOW_LEFT = "YellowArrowLeft", GREEN_LEFT = "GreenArrowLeft",
- RED_RIGHT = "RedArrowRight", YELLOW_RIGHT = "YellowArrowRight", GREEN_RIGHT = "GreenArrowRight",
- CROSS = "Cross", DONT_CROSS = "DontCross",
- STRAIGHT_RED = "StraightRed", STRAIGHT_YELLOW = "StraightYellow", STRAIGHT_GREEN = "StraightGreen",
- RED_UTURN = "RedArrowUTurn", YELLOW_UTURN = "YellowArrowUTurn", GREEN_UTURN = "GreenArrowUTurn",
- YELLOW_LEFT2 = "YellowArrowLeft2", YELLOW_RIGHT2 = "YellowArrowRight2", YELLOW_UTURN2 = "YellowArrowUTurn2"
- }
- -- === MOVEMENT SHORTCUTS ===
- MOVEMENT_BULBS = {
- straight = {
- GREEN = BULBS.GREEN,
- YELLOW = BULBS.YELLOW,
- RED = BULBS.RED
- },
- leftTurn = {
- GREEN = BULBS.GREEN_LEFT,
- YELLOW = BULBS.YELLOW_LEFT,
- RED = BULBS.RED_LEFT
- },
- rightTurn = {
- GREEN = BULBS.GREEN_RIGHT,
- YELLOW = BULBS.YELLOW_RIGHT,
- RED = BULBS.RED_RIGHT
- },
- uTurn = {
- GREEN = BULBS.GREEN_UTURN,
- YELLOW = BULBS.YELLOW_UTURN,
- RED = BULBS.RED_UTURN
- }
- }
- -- === TRAFFIC LIGHTS AND SENSORS ===
- local trafficLight = traffic.listBlockPos()
- local sensorList = traffic.listSensors()
- local phases = {}
- local pedestrianTriggers = {}
- local faultActive = false
- -- === LIGHT CONTROL ===
- function clearAllLights()
- for _, pos in ipairs(trafficLight) do
- traffic.clearStates(pos[1], pos[2], pos[3])
- end
- end
- function setLights(id,state,mode,forceClear)
- -- F is the boolean for Flashing, S is the boolean for Steady/On
- local f = false
- local s = false
- -- Clear signal if flag to forceClear isn't set to false
- if forceClear ~= false then
- if (type(id) == "table") then
- for itr = 1, #id do
- traffic.clearStates(trafficLight[id[itr]][1],trafficLight[id[itr]][2],trafficLight[id[itr]][3])
- end
- elseif (type(id) == "string") then
- traffic.clearStates(trafficLight[id][1],trafficLight[id][2],trafficLight[id][3])
- end
- end
- -- Determine mode
- if mode ~= nil then
- -- Flashing
- if string.lower(mode) == "flash" then
- s = true
- f = true
- -- Steady
- elseif string.lower(mode) == "on" then
- s = true
- f = false
- -- Off
- else
- s = false
- f = false
- end
- else
- s = false
- f = false
- end
- -- Apply to signal
- if (type(id) == "table" and state ~= nil) then
- for itr = 1, #id do
- traffic.setState(trafficLight[id[itr]][1],trafficLight[id[itr]][2],trafficLight[id[itr]][3],state,s,f)
- end
- elseif (type(id) == "string" and state ~= nil) then
- traffic.setState(trafficLight[id][1],trafficLight[id][2],trafficLight[id][3],state,s,f)
- end
- end
- function setLight(group, colorKey, mode, bulbsMap)
- local steady = mode == "on"
- local flashing = mode == "flash"
- for _, idx in ipairs(group) do
- local pos = trafficLight[idx]
- local bulbSet = bulbsMap and bulbsMap[idx]
- local targetBulb = bulbSet and bulbSet[colorKey:upper()]
- if targetBulb then
- traffic.setState(pos[1], pos[2], pos[3], targetBulb, false, false)
- traffic.setState(pos[1], pos[2], pos[3], targetBulb, steady, flashing)
- end
- end
- end
- function allRed(duration)
- duration = duration or 1
- for _, pos in ipairs(trafficLight) do
- traffic.clearStates(pos[1], pos[2], pos[3])
- traffic.setState(pos[1], pos[2], pos[3], BULBS.RED, true, false)
- traffic.setState(pos[1], pos[2], pos[3], BULBS.RED_LEFT, true, false)
- traffic.setState(pos[1], pos[2], pos[3], BULBS.RED_RIGHT, true, false)
- traffic.setState(pos[1], pos[2], pos[3], BULBS.STRAIGHT_RED, true, false)
- end
- os.sleep(duration)
- end
- function isSensorTripped(ids, w, h, l)
- w, h, l = w or 1, h or 1, l or 1
- for _, idx in ipairs(ids) do
- local pos = sensorList[idx]
- if pos and traffic.isSensorTripped(pos[1], pos[2], pos[3], w, h, l) then
- return true
- end
- end
- return false
- end
- function checkManualInputs()
- local bundle = rs.getBundledInput(sides.back)
- faultActive = bundle[colors.orange] and bundle[colors.orange] > 0
- end
- function setBundledOutput(color, value)
- rs.setBundledOutput(sides.back, {[color] = value})
- end
- -- === PHASE REGISTRATION ===
- function setPhase(data)
- local id = #phases + 1
- local speed = data.speedLimit or 40
- local yellowTime = math.max(3, math.ceil(speed / 20))
- local redTime = data.isLeftTurn and 2 or 1
- local alwaysOn = false
- if data.maxTime == nil and data.alwaysOn == nil then
- alwaysOn = true
- else
- alwaysOn = data.alwaysOn or false
- end
- phases[id] = {
- id = id,
- behavior = data.behavior or {},
- sensors = data.sensors or {},
- minTime = data.minTime or 10,
- maxTime = data.manTime,
- alwaysOn = alwaysOn,
- yellowTime = yellowTime,
- redTime = redTime,
- speedLimit = speed,
- forceColor = data.forceColor,
- always = data.always or false
- }
- end
- function otherPhaseWantsToRun(currentId)
- local bundle = rs.getBundledInput(sides.back)
- for _, p in ipairs(phases) do
- if p.id ~= currentId then
- if p.forceColor and bundle[p.forceColor] and bundle[p.forceColor] > 0 then return true end
- if p.sensors and #p.sensors > 0 and isSensorTripped(p.sensors, 2,4,2) then return true end
- end
- end
- return false
- end
- -- === FAULT MODE ===
- function runFaultMode()
- print("[FAULT MODE] Flashing lights...")
- while faultActive do
- clearAllLights()
- setLight({1, 2}, "Yellow", "flash")
- setLight({3}, "Red", "flash")
- setLight({4, 5}, "Red", "flash")
- os.sleep(1)
- checkManualInputs()
- end
- clearAllLights()
- print("[FAULT CLEARED]")
- end
- function isPhaseTriggered(phase, bundle)
- if phase.always then return true end
- if phase.forceColor and bundle[phase.forceColor] and bundle[phase.forceColor] > 0 then return true end
- if phase.sensors and #phase.sensors > 0 and isSensorTripped(phase.sensors, 2,4,2) then return true end
- return false
- end
- -- === MAIN PHASE LOOP ===
- function runPhases()
- allRed(2)
- while true do
- checkManualInputs()
- if faultActive then runFaultMode() end
- local bundle = rs.getBundledInput(sides.back)
- for _, phase in ipairs(phases) do
- checkManualInputs()
- if faultActive then break end
- local active = false
- local ped = pedestrianTriggers[phase.id]
- if phase.always then
- active = true
- elseif phase.forceColor and bundle[phase.forceColor] and bundle[phase.forceColor] > 0 then
- active = true
- elseif phase.sensors and #phase.sensors > 0 and isSensorTripped(phase.sensors, 2,4,2) then
- active = true
- elseif ped and bundle[ped.color] and bundle[ped.color] > 0 then
- active = true
- end
- if active then
- print("[PHASE] Green for phase " .. phase.id)
- for _, action in ipairs(phase.behavior.green or {}) do action() end
- local elapsed = 0
- local minTime = phase.minTime or phase.greenTime or 10
- local maxTime = phase.maxTime or phase.greenTime or 10
- local isAlways = phase.alwaysOn == true
- local wasInterrupted = false
- -- Wait minimum green time
- while elapsed < minTime do
- os.sleep(1)
- elapsed = elapsed + 1
- end
- -- Extended green time logic
- while true do
- checkManualInputs()
- bundle = rs.getBundledInput(sides.back)
- if isAlways then
- if otherPhaseWantsToRun(phase.id) then
- wasInterrupted = true
- break
- end
- else
- if not isPhaseTriggered(phase, bundle) then
- wasInterrupted = true
- break
- end
- if elapsed >= maxTime then break end
- end
- os.sleep(1)
- elapsed = elapsed + 1
- end
- -- Only run yellow/red if it was interrupted or not alwaysOn
- if wasInterrupted or not isAlways then
- if ped and bundle[ped.color] and bundle[ped.color] > 0 then
- print("[PEDESTRIAN] Crosswalk active")
- for _, action in ipairs(phase.behavior.red or {}) do action() end
- setBundledOutput(ped.color, 255)
- os.sleep(ped.time)
- setBundledOutput(ped.color, 0)
- end
- for _, action in ipairs(phase.behavior.yellow or {}) do action() end
- os.sleep(phase.yellowTime)
- for _, action in ipairs(phase.behavior.red or {}) do action() end
- os.sleep(phase.redTime)
- end
- end
- end
- os.sleep(0.1)
- end
- end
- -- === EXAMPLE PHASE SETUP ===
- -- phase 1
- setPhase({
- sensors = {1, 3},
- minTime = 10,
- maxTime = 25,
- speedLimit = 35,
- forceColor = colors.pink,
- isLeftTurn = true,
- behavior = {
- green = {
- function() setLights({1}, "GreenArrowLeft", "on") end,
- function() setLights({2,3}, "Green", "on") end,
- function() setLights(7, "GreenArrowRight", "on", false) end
- },
- yellow = {
- function() setLights(7, "GreenArrowRight", "off", false) end,
- function() setLights(7, "YellowArrowRight", "on", false) end,
- function() setLights({1}, "YellowArrowLeft", "on") end,
- },
- red = {
- function() setLights(7, "YellowArrowRight", "off", false) end,
- function() setLights({1}, "RedArrowLeft", "on") end,
- }
- }
- })
- setPhase({
- minTime = 30,
- alwaysOn = true,
- speedLimit = 35,
- always = true,
- behavior = {
- green = {
- function() setLights({2,3, 4,5}, "Green", "on") end,
- },
- yellow = {
- function() setLights({2,3, 4,5}, "Yellow", "on") end,
- },
- red = {
- function() setLights({2,3, 4,5}, "Red", "on") end,
- }
- }
- })
- setPhase({
- sensors = {2},
- minTime = 10,
- maxTime = 15,
- speedLimit = 25,
- forceColor = colors.lime,
- behavior = {
- green = {
- function() setLights({6,7}, "Green", "on") end,
- },
- yellow = {
- function() setLights({6,7}, "Yellow", "on") end,
- },
- red = {
- function() setLights({6,7}, "Red", "on") end,
- }
- }
- })
- -- === START ===
- print("Starting traffic controller...")
- thread.create(function() local ok, err = pcall(runPhases) if not ok then print("ERR:", err) end end)
- while true do os.sleep(1) end
Advertisement
Add Comment
Please, Sign In to add comment