/// <summary>
        /// Retrieves the custom traffic light at the given segment end.
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="segmentId"></param>
        /// <returns>existing custom traffic light at segment end, <code>null</code> if none exists</returns>
        public CustomSegmentLights GetSegmentLights(ushort segmentId, bool startNode, bool add = true, RoadBaseAI.TrafficLightState lightState = RoadBaseAI.TrafficLightState.Red)
        {
            if (!IsSegmentLight(segmentId, startNode))
            {
                if (add)
                {
                    return(AddSegmentLights(segmentId, startNode, lightState));
                }
                else
                {
                    return(null);
                }
            }

            CustomSegment customSegment = CustomSegments[segmentId];

            if (startNode)
            {
                return(customSegment.StartNodeLights);
            }
            else
            {
                return(customSegment.EndNodeLights);
            }
        }
        /// <summary>
        /// Removes the custom traffic light at the given segment end.
        /// </summary>
        /// <param name="segmentId"></param>
        /// <param name="startNode"></param>
        public void RemoveSegmentLight(ushort segmentId, bool startNode)
        {
#if DEBUG
            Log.Warning($"Removing segment light: {segmentId} @ startNode={startNode}");
#endif

            CustomSegment customSegment = CustomSegments[segmentId];
            if (customSegment == null)
            {
                return;
            }

            if (startNode)
            {
                customSegment.StartNodeLights = null;
            }
            else
            {
                customSegment.EndNodeLights = null;
            }

            if (customSegment.StartNodeLights == null && customSegment.EndNodeLights == null)
            {
                CustomSegments[segmentId] = null;
                UnsubscribeFromSegmentGeometry(segmentId);
            }
        }
        /// <summary>
        /// Removes the custom traffic light at the given segment end.
        /// </summary>
        /// <param name="segmentId">SegmentId affected</param>
        /// <param name="startNode">The nodeId affected</param>
        public void RemoveSegmentLight(ushort segmentId, bool startNode)
        {
#if DEBUG
            Log._Trace($"Removing segment light: {segmentId} @ startNode={startNode}");
#endif

            CustomSegment customSegment = customSegments_[segmentId];
            if (customSegment == null)
            {
                return;
            }

            if (startNode)
            {
                customSegment.StartNodeLights = null;
            }
            else
            {
                customSegment.EndNodeLights = null;
            }

            if (customSegment.StartNodeLights == null && customSegment.EndNodeLights == null)
            {
                customSegments_[segmentId] = null;
            }
        }
        public bool SetSegmentLights(ushort nodeId, ushort segmentId, ICustomSegmentLights lights)
        {
            SegmentEndGeometry endGeo = SegmentGeometry.Get(segmentId)?.GetEnd(nodeId);

            if (endGeo == null)
            {
                return(false);
            }

            CustomSegment customSegment = CustomSegments[segmentId];

            if (customSegment == null)
            {
                customSegment             = new CustomSegment();
                CustomSegments[segmentId] = customSegment;
            }

            if (endGeo.StartNode)
            {
                customSegment.StartNodeLights = lights;
            }
            else
            {
                customSegment.EndNodeLights = lights;
            }
            return(true);
        }
Beispiel #5
0
        /// <summary>
        /// Checks if a custom traffic light is present at the given segment end.
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="segmentId"></param>
        /// <returns></returns>
        public bool IsSegmentLight(ushort nodeId, ushort segmentId)
        {
#if TRACE
            Singleton <CodeProfiler> .instance.Start("CustomTrafficLights.IsSegmentLight");
#endif
            CustomSegment customSegment = CustomSegments[segmentId];
            if (customSegment == null)
            {
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.IsSegmentLight");
#endif
                return(false);
            }

            if (customSegment.Node1 == nodeId || customSegment.Node2 == nodeId)
            {
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.IsSegmentLight");
#endif
                return(true);
            }

#if TRACE
            Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.IsSegmentLight");
#endif
            return(false);
        }
        public bool SetSegmentLights(ushort nodeId,
                                     ushort segmentId,
                                     ICustomSegmentLights lights)
        {
            bool?startNode = Services.NetService.IsStartNode(segmentId, nodeId);

            if (startNode == null)
            {
                return(false);
            }

            CustomSegment customSegment = customSegments_[segmentId];

            if (customSegment == null)
            {
                customSegment = new CustomSegment();
                customSegments_[segmentId] = customSegment;
            }

            if ((bool)startNode)
            {
                customSegment.StartNodeLights = lights;
            }
            else
            {
                customSegment.EndNodeLights = lights;
            }

            return(true);
        }
Beispiel #7
0
        /// <summary>
        /// Performs a housekeeping operation for all custom traffic lights at the given segment.
        /// It is checked wheter the segment and connected nodes are still valid. If not, corresponding custom traffic lights are removed.
        /// </summary>
        /// <param name="segmentId"></param>
        private void Cleanup(ushort segmentId)
        {
#if TRACE
            Singleton <CodeProfiler> .instance.Start("CustomTrafficLights.Cleanup");
#endif
            CustomSegment customSegment = CustomSegments[segmentId];
            if (customSegment == null)
            {
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.Cleanup");
#endif
                return;
            }

            NetManager netManager = Singleton <NetManager> .instance;
            if (customSegment.Node1 != 0 && (netManager.m_nodes.m_buffer[customSegment.Node1].m_flags & (NetNode.Flags.Created | NetNode.Flags.Deleted)) != NetNode.Flags.Created)
            {
                customSegment.Node1       = 0;
                customSegment.Node1Lights = null;
            }

            if (customSegment.Node2 != 0 && (netManager.m_nodes.m_buffer[customSegment.Node2].m_flags & (NetNode.Flags.Created | NetNode.Flags.Deleted)) != NetNode.Flags.Created)
            {
                customSegment.Node2       = 0;
                customSegment.Node2Lights = null;
            }

            if (customSegment.Node1 == 0 && customSegment.Node2 == 0)
            {
                CustomSegments[segmentId] = null;
            }
#if TRACE
            Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.Cleanup");
#endif
        }
Beispiel #8
0
        /// <summary>
        /// Removes the custom traffic light at the given segment end.
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="segmentId"></param>
        public void RemoveSegmentLight(ushort nodeId, ushort segmentId)
        {
#if TRACE
            Singleton <CodeProfiler> .instance.Start("CustomTrafficLights.RemoveSegmentLight");
#endif
#if DEBUG
            Log.Warning($"Removing segment light: {segmentId} @ {nodeId}");
#endif

            CustomSegment customSegment = CustomSegments[segmentId];
            if (customSegment == null)
            {
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.RemoveSegmentLight");
#endif
                return;
            }

            if (customSegment.Node1 == nodeId)
            {
                customSegment.Node1       = 0;
                customSegment.Node1Lights = null;
            }
            else if (customSegment.Node2 == nodeId)
            {
                customSegment.Node2       = 0;
                customSegment.Node2Lights = null;
            }

            Cleanup(segmentId);
#if TRACE
            Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.RemoveSegmentLight");
#endif
        }
        AddSegmentLights(ushort segmentId,
                         bool startNode,
                         RoadBaseAI.TrafficLightState lightState =
                         RoadBaseAI.TrafficLightState.Red)
        {
#if DEBUG
            Log._Trace($"CustomTrafficLights.AddSegmentLights: Adding segment light: {segmentId} @ startNode={startNode}");
#endif
            if (!Services.NetService.IsSegmentValid(segmentId))
            {
                return(null);
            }

            CustomSegment customSegment = customSegments_[segmentId];

            if (customSegment == null)
            {
                customSegment = new CustomSegment();
                customSegments_[segmentId] = customSegment;
            }
            else
            {
                ICustomSegmentLights existingLights =
                    startNode ? customSegment.StartNodeLights : customSegment.EndNodeLights;

                if (existingLights != null)
                {
                    existingLights.SetLights(lightState);
                    return(existingLights);
                }
            }

            if (startNode)
            {
                customSegment.StartNodeLights = new CustomSegmentLights(
                    this,
                    segmentId,
                    startNode,
                    false);

                customSegment.StartNodeLights.SetLights(lightState);
                return(customSegment.StartNodeLights);
            }
            else
            {
                customSegment.EndNodeLights = new CustomSegmentLights(
                    this,
                    segmentId,
                    startNode,
                    false);

                customSegment.EndNodeLights.SetLights(lightState);
                return(customSegment.EndNodeLights);
            }
        }
        /// <summary>
        /// Checks if a custom traffic light is present at the given segment end.
        /// </summary>
        /// <param name="segmentId"></param>
        /// <param name="startNode"></param>
        /// <returns></returns>
        public bool IsSegmentLight(ushort segmentId, bool startNode)
        {
            CustomSegment customSegment = CustomSegments[segmentId];

            if (customSegment == null)
            {
                return(false);
            }

            return((startNode && customSegment.StartNodeLights != null) || (!startNode && customSegment.EndNodeLights != null));
        }
        /// <summary>
        /// Adds custom traffic lights at the specified node and segment.
        /// Light stats are set to the given light state, or to "Red" if no light state is given.
        /// </summary>
        /// <param name="segmentId"></param>
        /// <param name="startNode"></param>
        /// <param name="lightState">(optional) light state to set</param>
        public ICustomSegmentLights AddSegmentLights(ushort segmentId, bool startNode, RoadBaseAI.TrafficLightState lightState = RoadBaseAI.TrafficLightState.Red)
        {
#if DEBUG
            Log._Debug($"CustomTrafficLights.AddSegmentLights: Adding segment light: {segmentId} @ startNode={startNode}");
#endif

            SegmentGeometry segGeometry = SegmentGeometry.Get(segmentId);
            if (segGeometry == null)
            {
                Log.Error($"CustomTrafficLightsManager.AddSegmentLights: Segment {segmentId} is invalid.");
                return(null);
            }

            SegmentEndGeometry endGeometry = segGeometry.GetEnd(startNode);
            if (endGeometry == null)
            {
                Log.Error($"CustomTrafficLightsManager.AddSegmentLights: Segment {segmentId} is not connected to a node @ start {startNode}");
                return(null);
            }

            CustomSegment customSegment = CustomSegments[segmentId];
            if (customSegment == null)
            {
                customSegment             = new CustomSegment();
                CustomSegments[segmentId] = customSegment;
            }
            else
            {
                ICustomSegmentLights existingLights = startNode ? customSegment.StartNodeLights : customSegment.EndNodeLights;

                if (existingLights != null)
                {
                    existingLights.SetLights(lightState);
                    return(existingLights);
                }
            }

            SubscribeToSegmentGeometry(segmentId);
            if (startNode)
            {
                customSegment.StartNodeLights = new CustomSegmentLights(this, segmentId, startNode, false);
                customSegment.StartNodeLights.SetLights(lightState);
                return(customSegment.StartNodeLights);
            }
            else
            {
                customSegment.EndNodeLights = new CustomSegmentLights(this, segmentId, startNode, false);
                customSegment.EndNodeLights.SetLights(lightState);
                return(customSegment.EndNodeLights);
            }
        }
        GetSegmentLights(ushort segmentId,
                         bool startNode,
                         bool add = true,
                         RoadBaseAI.TrafficLightState lightState = RoadBaseAI.TrafficLightState.Red)
        {
            if (!IsSegmentLight(segmentId, startNode))
            {
                return(add ? AddSegmentLights(segmentId, startNode, lightState) : null);
            }

            CustomSegment customSegment = customSegments_[segmentId];

            return(startNode ? customSegment.StartNodeLights : customSegment.EndNodeLights);
        }
Beispiel #13
0
        /// <summary>
        /// Adds custom traffic lights at the specified node and segment.
        /// Light stats are set to the given light state, or to "Red" if no light state is given.
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="segmentId"></param>
        /// <param name="lightState">(optional) light state to set</param>
        public void AddSegmentLights(ushort nodeId, ushort segmentId, RoadBaseAI.TrafficLightState lightState = RoadBaseAI.TrafficLightState.Red)
        {
#if TRACE
            Singleton <CodeProfiler> .instance.Start("CustomTrafficLights.AddSegmentLights");
#endif
#if DEBUG
            Log._Debug($"CustomTrafficLights.AddSegmentLights: Adding segment light: {segmentId} @ {nodeId}");
#endif
            Cleanup(segmentId);

            CustomSegment customSegment = CustomSegments[segmentId];
            if (customSegment != null)
            {
                if (customSegment.Node1 == nodeId || customSegment.Node2 == nodeId)
                {
#if TRACE
                    Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.AddSegmentLights");
#endif
                    return;
                }

#if DEBUG
                Log._Debug($"CustomTrafficLights.AddSegmentLights: Adding segment light: {segmentId} @ {nodeId} -- Node1={CustomSegments[segmentId].Node1} Node2={CustomSegments[segmentId].Node2}");
#endif

                if (customSegment.Node1 == 0)
                {
                    customSegment.Node1Lights = new CustomSegmentLights(nodeId, segmentId, lightState);
                    customSegment.Node1       = nodeId;
                }
                else
                {
                    customSegment.Node2Lights = new CustomSegmentLights(nodeId, segmentId, lightState);
                    customSegment.Node2       = nodeId;
                }
            }
            else
            {
                customSegment             = new CustomSegment();
                customSegment.Node1Lights = new CustomSegmentLights(nodeId, segmentId, lightState);
                customSegment.Node1       = nodeId;
                CustomSegments[segmentId] = customSegment;
            }
#if TRACE
            Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.AddSegmentLights");
#endif
        }
        /// <summary>
        /// Adds custom traffic lights at the specified node and segment.
        /// Light stats are set to the given light state, or to "Red" if no light state is given.
        /// </summary>
        /// <param name="segmentId"></param>
        /// <param name="startNode"></param>
        /// <param name="lightState">(optional) light state to set</param>
        public CustomSegmentLights AddSegmentLights(ushort segmentId, bool startNode, RoadBaseAI.TrafficLightState lightState = RoadBaseAI.TrafficLightState.Red)
        {
#if DEBUG
            Log._Debug($"CustomTrafficLights.AddSegmentLights: Adding segment light: {segmentId} @ startNode={startNode}");
#endif
            CustomSegment customSegment = CustomSegments[segmentId];
            if (customSegment == null)
            {
                customSegment             = new CustomSegment();
                CustomSegments[segmentId] = customSegment;
            }
            else
            {
                CustomSegmentLights existingLights = startNode ? customSegment.StartNodeLights : customSegment.EndNodeLights;

                if (existingLights != null)
                {
                    existingLights.SetLights(lightState);
                    return(existingLights);
                }
            }

            SubscribeToSegmentGeometry(segmentId);
            if (startNode)
            {
                customSegment.StartNodeLights = new CustomSegmentLights(this, segmentId, startNode, false, lightState);
                customSegment.StartNodeLights.CalculateAutoPedestrianLightState();
                return(customSegment.StartNodeLights);
            }
            else
            {
                customSegment.EndNodeLights = new CustomSegmentLights(this, segmentId, startNode, false, lightState);
                customSegment.EndNodeLights.CalculateAutoPedestrianLightState();
                return(customSegment.EndNodeLights);
            }
        }
Beispiel #15
0
        /// <summary>
        /// Retrieves the custom traffic light at the given segment end.
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="segmentId"></param>
        /// <returns>existing custom traffic light at segment end, <code>null</code> if none exists</returns>
        public CustomSegmentLights GetSegmentLights(ushort nodeId, ushort segmentId)
        {
#if TRACE
            Singleton <CodeProfiler> .instance.Start("CustomTrafficLights.GetSegmentLights");
#endif
            CustomSegment customSegment = CustomSegments[segmentId];
            if (customSegment == null)
            {
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.GetSegmentLights");
#endif
                return(null);
            }

            //Log.Message($"Get segment light: {segmentId} @ {nodeId}");

            if (customSegment.Node1 == nodeId)
            {
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.GetSegmentLights");
#endif
                return(customSegment.Node1Lights);
            }
            if (customSegment.Node2 == nodeId)
            {
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.GetSegmentLights");
#endif
                return(customSegment.Node2Lights);
            }

#if TRACE
            Singleton <CodeProfiler> .instance.Stop("CustomTrafficLights.GetSegmentLights");
#endif
            return(null);
        }