AddLiveSegmentLights() static private method

static private AddLiveSegmentLights ( ushort nodeId, ushort segmentId ) : void
nodeId ushort
segmentId ushort
return void
Beispiel #1
0
        public TimedTrafficLights(ushort nodeId, IEnumerable <ushort> nodeGroup)
        {
            this.NodeId  = nodeId;
            NodeGroup    = new List <ushort>(nodeGroup);
            masterNodeId = NodeGroup[0];

            // setup priority segments & live traffic lights
            foreach (ushort slaveNodeId in nodeGroup)
            {
                for (int s = 0; s < 8; ++s)
                {
                    ushort segmentId = Singleton <NetManager> .instance.m_nodes.m_buffer[slaveNodeId].GetSegment(s);
                    if (segmentId <= 0)
                    {
                        continue;
                    }
                    CustomRoadAI.GetSegmentGeometry(segmentId).Recalculate(true, true);
                    TrafficPriority.AddPrioritySegment(slaveNodeId, segmentId, SegmentEnd.PriorityType.None);
                    CustomTrafficLights.AddLiveSegmentLights(slaveNodeId, segmentId);
                }
            }

            started = false;
        }
Beispiel #2
0
        /*public static TimedTrafficLights AddTimedLight(ushort nodeid, List<ushort> nodeGroup, bool vehiclesMayEnterBlockedJunctions) {
         *      TimedScripts.Add(nodeid, new TimedTrafficLights(nodeid, nodeGroup, vehiclesMayEnterBlockedJunctions));
         *      return TimedScripts[nodeid];
         * }
         *
         * public static void RemoveTimedLight(ushort nodeid) {
         *      TimedScripts.Remove(nodeid);
         * }
         *
         * public static bool IsTimedLight(ushort nodeid) {
         *      return TimedScripts.ContainsKey(nodeid);
         * }
         *
         * public static TimedTrafficLights GetTimedLight(ushort nodeid) {
         *      if (!IsTimedLight(nodeid))
         *              return null;
         *      return TimedScripts[nodeid];
         * }
         *
         * internal static void OnLevelUnloading() {
         *      TimedScripts.Clear();
         * }*/

        internal void handleNewSegments()
        {
            if (NumSteps() <= 0)
            {
                // no steps defined, just create live traffic lights
                for (int s = 0; s < 8; ++s)
                {
                    ushort segmentId = Singleton <NetManager> .instance.m_nodes.m_buffer[NodeId].GetSegment(s);
                    if (segmentId <= 0)
                    {
                        continue;
                    }
                    CustomTrafficLights.AddLiveSegmentLights(NodeId, segmentId);
                }

                return;
            }

            for (int s = 0; s < 8; ++s)
            {
                ushort segmentId = Singleton <NetManager> .instance.m_nodes.m_buffer[NodeId].GetSegment(s);
                if (segmentId <= 0)
                {
                    continue;
                }

                List <ushort> invalidSegmentIds = new List <ushort>();
                bool          isNewSegment      = true;

                foreach (KeyValuePair <ushort, CustomSegmentLights> e in Steps[0].segmentLights)
                {
                    var fromSegmentId = e.Key;

                    if (fromSegmentId == segmentId)
                    {
                        isNewSegment = false;
                    }

                    if (!TrafficPriority.IsPrioritySegment(NodeId, fromSegmentId))
                    {
                        invalidSegmentIds.Add(fromSegmentId);
                    }
                }

                if (isNewSegment)
                {
                    Log._Debug($"New segment detected: {segmentId} @ {NodeId}");
                    // segment was created
                    CustomTrafficLights.AddLiveSegmentLights(NodeId, segmentId);
                    TrafficPriority.AddPrioritySegment(NodeId, segmentId, SegmentEnd.PriorityType.None);

                    if (invalidSegmentIds.Count > 0)
                    {
                        var oldSegmentId = invalidSegmentIds[0];
                        TrafficPriority.RemovePrioritySegment(NodeId, oldSegmentId);
                        Log._Debug($"Replacing old segment {oldSegmentId} @ {NodeId} with new segment {segmentId}");

                        // replace the old segment with the newly created one
                        for (int i = 0; i < NumSteps(); ++i)
                        {
                            CustomSegmentLights segmentLights = Steps[i].segmentLights[oldSegmentId];
                            Steps[i].segmentLights.Remove(oldSegmentId);
                            segmentLights.SegmentId = segmentId;
                            Steps[i].segmentLights.Add(segmentId, segmentLights);
                            Steps[i].calcMaxSegmentLength();
                            CustomSegmentLights liveSegLights = CustomTrafficLights.GetSegmentLights(NodeId, segmentId);
                            foreach (KeyValuePair <ExtVehicleType, CustomSegmentLight> e in segmentLights.CustomLights)
                            {
                                CustomSegmentLight liveSegLight = liveSegLights.GetCustomLight(e.Key);
                                if (liveSegLight == null)
                                {
                                    continue;
                                }
                                liveSegLight.CurrentMode = e.Value.CurrentMode;
                            }
                        }
                    }
                    else
                    {
                        Log._Debug($"Adding new segment {segmentId} to node {NodeId}");

                        // create a new manual light
                        for (int i = 0; i < NumSteps(); ++i)
                        {
                            Steps[i].addSegment(segmentId, true);
                            Steps[i].calcMaxSegmentLength();
                        }
                    }
                }
            }
        }
Beispiel #3
0
        internal bool housekeeping(bool housekeepCustomLights)
        {
            bool          mayStart        = true;
            List <ushort> nodeIdsToDelete = new List <ushort>();

            int i = 0;

            foreach (ushort otherNodeId in NodeGroup)
            {
                if ((Singleton <NetManager> .instance.m_nodes.m_buffer[otherNodeId].m_flags & NetNode.Flags.Created) == NetNode.Flags.None)
                {
                    Log.Warning($"Timed housekeeping: Remove node {otherNodeId}");
                    nodeIdsToDelete.Add(otherNodeId);
                    if (otherNodeId == NodeId)
                    {
                        Log.Warning($"Timed housekeeping: Other is this. mayStart = false");
                        mayStart = false;
                    }
                }
                ++i;
            }

            foreach (ushort nodeIdToDelete in nodeIdsToDelete)
            {
                NodeGroup.Remove(nodeIdToDelete);
                TrafficLightSimulation.RemoveNodeFromSimulation(nodeIdToDelete, false);
            }

            // check that live lights exist (TODO refactor?)
            foreach (ushort timedNodeId in NodeGroup)
            {
                for (int s = 0; s < 8; s++)
                {
                    var segmentId = Singleton <NetManager> .instance.m_nodes.m_buffer[timedNodeId].GetSegment(s);

                    if (segmentId == 0)
                    {
                        continue;
                    }
                    CustomTrafficLights.AddLiveSegmentLights(timedNodeId, segmentId);
                }
            }

            if (NodeGroup.Count <= 0)
            {
                Log.Warning($"Timed housekeeping: No lights left. mayStart = false");
                mayStart = false;
                return(mayStart);
            }
            //Log.Warning($"Timed housekeeping: Setting master node to {NodeGroup[0]}");
            masterNodeId = NodeGroup[0];

            if (housekeepCustomLights)
            {
                foreach (TimedTrafficLightsStep step in Steps)
                {
                    foreach (KeyValuePair <ushort, CustomSegmentLights> e in step.segmentLights)
                    {
                        e.Value.housekeeping(true);
                    }
                }
            }

            return(mayStart);
        }