GamearooCodes

Untitled

May 13th, 2025
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.60 KB | None | 0 0
  1. -- === COMPONENT SETUP ===
  2. local component = require("component")
  3. local traffic = component.traffic_light_card
  4. local os = require("os")
  5. local thread = require("thread")
  6. local sides = require("sides")
  7. local colors = require("colors")
  8. local rs = component.redstone
  9.  
  10. -- === BULB DEFINITIONS ===
  11. BULBS = {
  12.   RED = "Red", YELLOW = "Yellow", GREEN = "Green",
  13.   RED_LEFT = "RedArrowLeft", YELLOW_LEFT = "YellowArrowLeft", GREEN_LEFT = "GreenArrowLeft",
  14.   RED_RIGHT = "RedArrowRight", YELLOW_RIGHT = "YellowArrowRight", GREEN_RIGHT = "GreenArrowRight",
  15.   CROSS = "Cross", DONT_CROSS = "DontCross",
  16.   STRAIGHT_RED = "StraightRed", STRAIGHT_YELLOW = "StraightYellow", STRAIGHT_GREEN = "StraightGreen",
  17.   RED_UTURN = "RedArrowUTurn", YELLOW_UTURN = "YellowArrowUTurn", GREEN_UTURN = "GreenArrowUTurn",
  18.   YELLOW_LEFT2 = "YellowArrowLeft2", YELLOW_RIGHT2 = "YellowArrowRight2", YELLOW_UTURN2 = "YellowArrowUTurn2"
  19. }
  20.  
  21. -- === MOVEMENT SHORTCUTS ===
  22. MOVEMENT_BULBS = {
  23.   straight = {
  24.     GREEN = BULBS.GREEN,
  25.     YELLOW = BULBS.YELLOW,
  26.     RED = BULBS.RED
  27.   },
  28.   leftTurn = {
  29.     GREEN = BULBS.GREEN_LEFT,
  30.     YELLOW = BULBS.YELLOW_LEFT,
  31.     RED = BULBS.RED_LEFT
  32.   },
  33.   rightTurn = {
  34.     GREEN = BULBS.GREEN_RIGHT,
  35.     YELLOW = BULBS.YELLOW_RIGHT,
  36.     RED = BULBS.RED_RIGHT
  37.   },
  38.   uTurn = {
  39.     GREEN = BULBS.GREEN_UTURN,
  40.     YELLOW = BULBS.YELLOW_UTURN,
  41.     RED = BULBS.RED_UTURN
  42.   }
  43. }
  44.  
  45. -- === TRAFFIC LIGHTS AND SENSORS ===
  46. local trafficLight = traffic.listBlockPos()
  47. local sensorList = traffic.listSensors()
  48. local phases = {}
  49. local pedestrianTriggers = {}
  50. local faultActive = false
  51.  
  52. -- === LIGHT CONTROL ===
  53. function clearAllLights()
  54.   for _, pos in ipairs(trafficLight) do
  55.     traffic.clearStates(pos[1], pos[2], pos[3])
  56.   end
  57. end
  58.  
  59. function setLights(id,state,mode,forceClear)
  60.   -- F is the boolean for Flashing, S is the boolean for Steady/On
  61.   local f = false
  62.   local s = false
  63.  
  64.   --  Clear signal if flag to forceClear isn't set to false
  65.   if forceClear ~= false then
  66.     if (type(id) == "table") then
  67.       for itr = 1, #id do
  68.         traffic.clearStates(trafficLight[id[itr]][1],trafficLight[id[itr]][2],trafficLight[id[itr]][3])
  69.       end
  70.     elseif (type(id) == "string") then
  71.       traffic.clearStates(trafficLight[id][1],trafficLight[id][2],trafficLight[id][3])
  72.     end
  73.   end
  74.  
  75.   --  Determine mode
  76.   if mode ~= nil then
  77.     --    Flashing
  78.     if string.lower(mode) == "flash" then
  79.       s = true
  80.       f = true
  81.     --   Steady
  82.     elseif string.lower(mode) == "on" then
  83.       s = true
  84.       f = false
  85.       --    Off
  86.     else
  87.       s = false
  88.       f = false
  89.     end
  90.   else
  91.     s = false
  92.     f = false
  93.   end
  94.  
  95.   --  Apply to signal
  96.   if (type(id) == "table" and state ~= nil) then
  97.     for itr = 1, #id do
  98.       traffic.setState(trafficLight[id[itr]][1],trafficLight[id[itr]][2],trafficLight[id[itr]][3],state,s,f)
  99.     end
  100.  
  101.   elseif (type(id) == "string" and state ~= nil) then
  102.     traffic.setState(trafficLight[id][1],trafficLight[id][2],trafficLight[id][3],state,s,f)
  103.   end
  104. end
  105.  
  106. function setLight(group, colorKey, mode, bulbsMap)
  107.   local steady = mode == "on"
  108.   local flashing = mode == "flash"
  109.  
  110.   for _, idx in ipairs(group) do
  111.     local pos = trafficLight[idx]
  112.     local bulbSet = bulbsMap and bulbsMap[idx]
  113.     local targetBulb = bulbSet and bulbSet[colorKey:upper()]
  114.     if targetBulb then
  115.       traffic.setState(pos[1], pos[2], pos[3], targetBulb, false, false)
  116.       traffic.setState(pos[1], pos[2], pos[3], targetBulb, steady, flashing)
  117.     end
  118.   end
  119. end
  120.  
  121. function allRed(duration)
  122.   duration = duration or 1
  123.   for _, pos in ipairs(trafficLight) do
  124.     traffic.clearStates(pos[1], pos[2], pos[3])
  125.     traffic.setState(pos[1], pos[2], pos[3], BULBS.RED, true, false)
  126.     traffic.setState(pos[1], pos[2], pos[3], BULBS.RED_LEFT, true, false)
  127.     traffic.setState(pos[1], pos[2], pos[3], BULBS.RED_RIGHT, true, false)
  128.     traffic.setState(pos[1], pos[2], pos[3], BULBS.STRAIGHT_RED, true, false)
  129.   end
  130.   os.sleep(duration)
  131. end
  132.  
  133. function isSensorTripped(ids, w, h, l)
  134.   w, h, l = w or 1, h or 1, l or 1
  135.   for _, idx in ipairs(ids) do
  136.     local pos = sensorList[idx]
  137.     if pos and traffic.isSensorTripped(pos[1], pos[2], pos[3], w, h, l) then
  138.       return true
  139.     end
  140.   end
  141.   return false
  142. end
  143.  
  144. function checkManualInputs()
  145.   local bundle = rs.getBundledInput(sides.back)
  146.   faultActive = bundle[colors.orange] and bundle[colors.orange] > 0
  147. end
  148.  
  149. function setBundledOutput(color, value)
  150.   rs.setBundledOutput(sides.back, {[color] = value})
  151. end
  152.  
  153. -- === PHASE REGISTRATION ===
  154. function setPhase(data)
  155.   local id = #phases + 1
  156.   local speed = data.speedLimit or 40
  157.   local yellowTime = math.max(3, math.ceil(speed / 20))
  158.   local redTime = data.isLeftTurn and 2 or 1
  159. local alwaysOn = false
  160.    if data.maxTime == nil and data.alwaysOn == nil then
  161.     alwaysOn = true
  162.   else
  163.     alwaysOn = data.alwaysOn or false
  164.   end
  165.  
  166.   phases[id] = {
  167.     id = id,
  168.     behavior = data.behavior or {},
  169.     sensors = data.sensors or {},
  170.     minTime = data.minTime or 10,
  171.     maxTime = data.manTime,
  172.     alwaysOn = alwaysOn,
  173.     yellowTime = yellowTime,
  174.     redTime = redTime,
  175.     speedLimit = speed,
  176.     forceColor = data.forceColor,
  177.     always = data.always or false
  178.   }
  179. end
  180.  
  181.  
  182.  
  183. function otherPhaseWantsToRun(currentId)
  184.   local bundle = rs.getBundledInput(sides.back)
  185.   for _, p in ipairs(phases) do
  186.     if p.id ~= currentId then
  187.       if p.forceColor and bundle[p.forceColor] and bundle[p.forceColor] > 0 then return true end
  188.       if p.sensors and #p.sensors > 0 and isSensorTripped(p.sensors, 2,4,2) then return true end
  189.     end
  190.   end
  191.   return false
  192. end
  193.  
  194.  
  195. -- === FAULT MODE ===
  196. function runFaultMode()
  197.   print("[FAULT MODE] Flashing lights...")
  198.   while faultActive do
  199.     clearAllLights()
  200.     setLight({1, 2}, "Yellow", "flash")
  201.     setLight({3}, "Red", "flash")
  202.     setLight({4, 5}, "Red", "flash")
  203.     os.sleep(1)
  204.     checkManualInputs()
  205.   end
  206.   clearAllLights()
  207.   print("[FAULT CLEARED]")
  208. end
  209. function isPhaseTriggered(phase, bundle)
  210.   if phase.always then return true end
  211.   if phase.forceColor and bundle[phase.forceColor] and bundle[phase.forceColor] > 0 then return true end
  212.   if phase.sensors and #phase.sensors > 0 and isSensorTripped(phase.sensors, 2,4,2) then return true end
  213.   return false
  214. end
  215.  
  216.  
  217. -- === MAIN PHASE LOOP ===
  218. function runPhases()
  219.   allRed(2)
  220.  
  221.   while true do
  222.     checkManualInputs()
  223.     if faultActive then runFaultMode() end
  224.  
  225.     local bundle = rs.getBundledInput(sides.back)
  226.  
  227.     for _, phase in ipairs(phases) do
  228.       checkManualInputs()
  229.       if faultActive then break end
  230.  
  231.       local active = false
  232.       local ped = pedestrianTriggers[phase.id]
  233.  
  234.       if phase.always then
  235.         active = true
  236.       elseif phase.forceColor and bundle[phase.forceColor] and bundle[phase.forceColor] > 0 then
  237.         active = true
  238.       elseif phase.sensors and #phase.sensors > 0 and isSensorTripped(phase.sensors, 2,4,2) then
  239.         active = true
  240.       elseif ped and bundle[ped.color] and bundle[ped.color] > 0 then
  241.         active = true
  242.       end
  243.  
  244.       if active then
  245.         print("[PHASE] Green for phase " .. phase.id)
  246.         for _, action in ipairs(phase.behavior.green or {}) do action() end
  247.  
  248.         local elapsed = 0
  249.         local minTime = phase.minTime or phase.greenTime or 10
  250.         local maxTime = phase.maxTime or phase.greenTime or 10
  251.         local isAlways = phase.alwaysOn == true
  252.         local wasInterrupted = false
  253.  
  254.         -- Wait minimum green time
  255.         while elapsed < minTime do
  256.           os.sleep(1)
  257.           elapsed = elapsed + 1
  258.         end
  259.  
  260.         -- Extended green time logic
  261.         while true do
  262.           checkManualInputs()
  263.           bundle = rs.getBundledInput(sides.back)
  264.  
  265.           if isAlways then
  266.             if otherPhaseWantsToRun(phase.id) then
  267.               wasInterrupted = true
  268.               break
  269.             end
  270.           else
  271.             if not isPhaseTriggered(phase, bundle) then
  272.               wasInterrupted = true
  273.               break
  274.             end
  275.             if elapsed >= maxTime then break end
  276.           end
  277.  
  278.           os.sleep(1)
  279.           elapsed = elapsed + 1
  280.         end
  281.  
  282.         -- Only run yellow/red if it was interrupted or not alwaysOn
  283.         if wasInterrupted or not isAlways then
  284.           if ped and bundle[ped.color] and bundle[ped.color] > 0 then
  285.             print("[PEDESTRIAN] Crosswalk active")
  286.             for _, action in ipairs(phase.behavior.red or {}) do action() end
  287.             setBundledOutput(ped.color, 255)
  288.             os.sleep(ped.time)
  289.             setBundledOutput(ped.color, 0)
  290.           end
  291.  
  292.           for _, action in ipairs(phase.behavior.yellow or {}) do action() end
  293.           os.sleep(phase.yellowTime)
  294.  
  295.           for _, action in ipairs(phase.behavior.red or {}) do action() end
  296.           os.sleep(phase.redTime)
  297.         end
  298.       end
  299.     end
  300.  
  301.     os.sleep(0.1)
  302.   end
  303. end
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312. -- === EXAMPLE PHASE SETUP ===
  313.  
  314. -- phase 1
  315. setPhase({
  316.   sensors = {1, 3},
  317.   minTime = 10,
  318.   maxTime = 25,
  319.   speedLimit = 35,
  320.   forceColor = colors.pink,
  321.   isLeftTurn = true,
  322.   behavior = {
  323.     green = {
  324.       function() setLights({1}, "GreenArrowLeft", "on") end,
  325.       function() setLights({2,3}, "Green", "on") end,
  326.       function() setLights(7, "GreenArrowRight", "on", false) end
  327.     },
  328.     yellow = {
  329.       function() setLights(7, "GreenArrowRight", "off", false) end,
  330.       function() setLights(7, "YellowArrowRight", "on", false) end,
  331.       function() setLights({1}, "YellowArrowLeft", "on") end,
  332.     },
  333.     red = {
  334.      function() setLights(7, "YellowArrowRight", "off", false) end,
  335.      
  336.       function() setLights({1}, "RedArrowLeft", "on") end,
  337.     }
  338.   }
  339. })
  340.  
  341.  
  342.  
  343. setPhase({
  344.  
  345.   minTime = 30,
  346.   alwaysOn = true,
  347.   speedLimit = 35,
  348.   always = true,
  349.   behavior = {
  350.     green = {
  351.    
  352.       function() setLights({2,3, 4,5}, "Green", "on") end,
  353.      
  354.     },
  355.     yellow = {
  356.       function() setLights({2,3, 4,5}, "Yellow", "on") end,
  357.     },
  358.     red = {
  359.     function() setLights({2,3, 4,5}, "Red", "on") end,
  360.     }
  361.   }
  362. })
  363.  
  364.  
  365.  
  366. setPhase({
  367.   sensors = {2},
  368.   minTime = 10,
  369.   maxTime = 15,
  370.   speedLimit = 25,
  371.   forceColor = colors.lime,
  372.   behavior = {
  373.     green = {
  374.    
  375.       function() setLights({6,7}, "Green", "on") end,
  376.      
  377.     },
  378.     yellow = {
  379.       function() setLights({6,7}, "Yellow", "on") end,
  380.     },
  381.     red = {
  382.     function() setLights({6,7}, "Red", "on") end,
  383.     }
  384.   }
  385. })
  386.  
  387. -- === START ===
  388. print("Starting traffic controller...")
  389. thread.create(function() local ok, err = pcall(runPhases) if not ok then print("ERR:", err) end end)
  390. while true do os.sleep(1) end
  391.  
Advertisement
Add Comment
Please, Sign In to add comment