GetOrLiveSegmentLights() public static method

public static GetOrLiveSegmentLights ( ushort nodeId, ushort segmentId ) : CustomSegmentLights
nodeId ushort
segmentId ushort
return CustomSegmentLights
        public void Start()
        {
            /*if (!housekeeping())
             *      return;*/

            for (int s = 0; s < 8; ++s)
            {
                ushort segmentId = Singleton <NetManager> .instance.m_nodes.m_buffer[NodeId].GetSegment(s);
                if (segmentId == 0)
                {
                    continue;
                }
                bool needsAlwaysGreenPedestrian = true;
                foreach (TimedTrafficLightsStep step in Steps)
                {
                    if (!step.segmentLights.ContainsKey(segmentId))
                    {
                        continue;
                    }
                    if (step.segmentLights[segmentId].PedestrianLightState == RoadBaseAI.TrafficLightState.Green)
                    {
                        needsAlwaysGreenPedestrian = false;
                        break;
                    }
                }

                CustomTrafficLights.GetOrLiveSegmentLights(NodeId, segmentId).InvalidPedestrianLight = needsAlwaysGreenPedestrian;
            }

            CurrentStep = 0;
            Steps[0].Start();
            Steps[0].SetLights();

            started = true;
        }
 /// <summary>
 /// Adds a new segment to this step. After adding all steps the method `rebuildSegmentIds` must be called.
 /// </summary>
 /// <param name="segmentId"></param>
 internal void addSegment(ushort segmentId, bool makeRed)
 {
     segmentLights.Add(segmentId, (CustomSegmentLights)CustomTrafficLights.GetOrLiveSegmentLights(timedNode.NodeId, segmentId).Clone());
     if (makeRed)
     {
         segmentLights[segmentId].MakeRed();
     }
     else
     {
         segmentLights[segmentId].MakeRedOrGreen();
     }
 }
        public void SetLights(bool noTransition)
        {
#if TRACE
            Singleton <CodeProfiler> .instance.Start("TimedTrafficLightsStep.SetLights");
#endif
            try {
                bool atEndTransition   = !noTransition && isInEndTransition();                       // = yellow
                bool atStartTransition = !noTransition && !atEndTransition && isInStartTransition(); // = red + yellow

#if DEBUG
                if (timedNode == null)
                {
                    Log.Error($"TimedTrafficLightsStep: timedNode is null!");
#if TRACE
                    Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                    return;
                }
#endif

                TimedTrafficLightsStep previousStep = timedNode.Steps[(timedNode.CurrentStep + timedNode.Steps.Count - 1) % timedNode.Steps.Count];
                TimedTrafficLightsStep nextStep     = timedNode.Steps[(timedNode.CurrentStep + 1) % timedNode.Steps.Count];

#if DEBUG
                if (previousStep == null)
                {
                    Log.Error($"TimedTrafficLightsStep: previousStep is null!");
#if TRACE
                    Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                    return;
                }

                if (nextStep == null)
                {
                    Log.Error($"TimedTrafficLightsStep: nextStep is null!");
#if TRACE
                    Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                    return;
                }

                if (previousStep.segmentLights == null)
                {
                    Log.Error($"TimedTrafficLightsStep: previousStep.segmentLights is null!");
                    return;
                }

                if (nextStep.segmentLights == null)
                {
                    Log.Error($"TimedTrafficLightsStep: nextStep.segmentLights is null!");
#if TRACE
                    Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                    return;
                }

                if (segmentLights == null)
                {
                    Log.Error($"TimedTrafficLightsStep: segmentLights is null!");
#if TRACE
                    Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                    return;
                }
#endif

                foreach (KeyValuePair <ushort, CustomSegmentLights> e in segmentLights)
                {
                    var segmentId            = e.Key;
                    var curStepSegmentLights = e.Value;

#if DEBUG
                    if (!previousStep.segmentLights.ContainsKey(segmentId))
                    {
                        Log.Error($"TimedTrafficLightsStep: previousStep does not contain lights for segment {segmentId}!");
#if TRACE
                        Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                        return;
                    }

                    if (!nextStep.segmentLights.ContainsKey(segmentId))
                    {
                        Log.Error($"TimedTrafficLightsStep: nextStep does not contain lights for segment {segmentId}!");
#if TRACE
                        Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                        return;
                    }
#endif

                    var prevStepSegmentLights = previousStep.segmentLights[segmentId];
                    var nextStepSegmentLights = nextStep.segmentLights[segmentId];

                    //segLightState.makeRedOrGreen(); // TODO temporary fix

                    var liveSegmentLights = CustomTrafficLights.GetOrLiveSegmentLights(timedNode.NodeId, segmentId);
                    if (liveSegmentLights == null)
                    {
                        continue;
                    }

                    if (curStepSegmentLights.PedestrianLightState != null && prevStepSegmentLights.PedestrianLightState != null && nextStepSegmentLights.PedestrianLightState != null)
                    {
                        RoadBaseAI.TrafficLightState pedLightState = calcLightState((RoadBaseAI.TrafficLightState)prevStepSegmentLights.PedestrianLightState, (RoadBaseAI.TrafficLightState)curStepSegmentLights.PedestrianLightState, (RoadBaseAI.TrafficLightState)nextStepSegmentLights.PedestrianLightState, atStartTransition, atEndTransition);
                        //Log._Debug($"TimedStep.SetLights: Setting pedestrian light state @ seg. {segmentId} to {pedLightState} {curStepSegmentLights.ManualPedestrianMode}");
                        liveSegmentLights.ManualPedestrianMode = curStepSegmentLights.ManualPedestrianMode;
                        liveSegmentLights.PedestrianLightState = pedLightState;
                        //Log._Debug($"Step @ {timedNode.NodeId}: Segment {segmentId}: Ped.: {liveSegmentLights.PedestrianLightState.ToString()}");
                    }

#if DEBUG
                    if (curStepSegmentLights.VehicleTypes == null)
                    {
                        Log.Error($"TimedTrafficLightsStep: curStepSegmentLights.VehicleTypes is null!");
#if TRACE
                        Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                        return;
                    }
#endif

                    foreach (ExtVehicleType vehicleType in curStepSegmentLights.VehicleTypes)
                    {
                        CustomSegmentLight liveSegmentLight = liveSegmentLights.GetCustomLight(vehicleType);
                        if (liveSegmentLight == null)
                        {
#if DEBUG
                            Log._Debug($"Timed step @ seg. {segmentId}, node {timedNode.NodeId} has a traffic light for {vehicleType} but the live segment does not have one.");
#endif
                            continue;
                        }
                        CustomSegmentLight curStepSegmentLight  = curStepSegmentLights.GetCustomLight(vehicleType);
                        CustomSegmentLight prevStepSegmentLight = prevStepSegmentLights.GetCustomLight(vehicleType);
                        CustomSegmentLight nextStepSegmentLight = nextStepSegmentLights.GetCustomLight(vehicleType);
#if DEBUG
                        if (curStepSegmentLight == null)
                        {
                            Log.Error($"TimedTrafficLightsStep: curStepSegmentLight is null!");
#if TRACE
                            Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                            return;
                        }

                        if (prevStepSegmentLight == null)
                        {
                            Log.Error($"TimedTrafficLightsStep: prevStepSegmentLight is null!");
#if TRACE
                            Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                            return;
                        }

                        if (nextStepSegmentLight == null)
                        {
                            Log.Error($"TimedTrafficLightsStep: nextStepSegmentLight is null!");
#if TRACE
                            Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
                            return;
                        }
#endif

                        liveSegmentLight.CurrentMode = curStepSegmentLight.CurrentMode;
                        liveSegmentLight.LightMain   = calcLightState(prevStepSegmentLight.LightMain, curStepSegmentLight.LightMain, nextStepSegmentLight.LightMain, atStartTransition, atEndTransition);
                        liveSegmentLight.LightLeft   = calcLightState(prevStepSegmentLight.LightLeft, curStepSegmentLight.LightLeft, nextStepSegmentLight.LightLeft, atStartTransition, atEndTransition);
                        liveSegmentLight.LightRight  = calcLightState(prevStepSegmentLight.LightRight, curStepSegmentLight.LightRight, nextStepSegmentLight.LightRight, atStartTransition, atEndTransition);

                        //Log._Debug($"Step @ {timedNode.NodeId}: Segment {segmentId} for vehicle type {vehicleType}: L: {liveSegmentLight.LightLeft.ToString()} F: {liveSegmentLight.LightMain.ToString()} R: {liveSegmentLight.LightRight.ToString()}");
                    }

                    /*if (timedNode.NodeId == 20164) {
                     *      Log._Debug($"Step @ {timedNode.NodeId}: Segment {segmentId}: {segmentLight.LightLeft.ToString()} {segmentLight.LightMain.ToString()} {segmentLight.LightRight.ToString()} {segmentLight.LightPedestrian.ToString()}");
                     * }*/

                    liveSegmentLights.UpdateVisuals();
                }
            } catch (Exception e) {
                Log.Error($"Exception in TimedTrafficStep.SetLights: {e.ToString()}");
                invalid = true;
            }
#if TRACE
            Singleton <CodeProfiler> .instance.Stop("TimedTrafficLightsStep.SetLights");
#endif
        }