RemoveNodeFromSimulation() public static method

public static RemoveNodeFromSimulation ( ushort nodeId, bool destroyGroup ) : void
nodeId ushort
destroyGroup bool
return void
Exemplo n.º 1
0
 internal void RemoveNodeFromGroup(ushort otherNodeId)
 {
     NodeGroup.Remove(otherNodeId);
     if (NodeGroup.Count <= 0)
     {
         TrafficLightSimulation.RemoveNodeFromSimulation(NodeId, true);
         return;
     }
     masterNodeId = NodeGroup[0];
 }
        public void OnUpdate(NodeGeometry nodeGeometry)
        {
#if DEBUG
            Log._Debug($"TrafficLightSimulation: OnUpdate @ node {NodeId} ({nodeGeometry.NodeId})");
#endif

            if (!Flags.mayHaveTrafficLight(NodeId))
            {
                Log.Warning($"Housekeeping: Node {NodeId} has traffic light simulation but must not have a traffic light!");
                TrafficLightSimulation.RemoveNodeFromSimulation(NodeId, false, true);
            }

            if (!IsManualLight() && !IsTimedLight())
            {
                return;
            }

            if (!nodeGeometry.IsValid())
            {
                // node has become invalid. Remove manual/timed traffic light and destroy custom lights
                RemoveNodeFromSimulation(NodeId, false, false);
                return;
            }

            for (var s = 0; s < 8; s++)
            {
                var segmentId = Singleton <NetManager> .instance.m_nodes.m_buffer[NodeId].GetSegment(s);

                if (segmentId == 0)
                {
                    continue;
                }

#if DEBUG
                Log._Debug($"TrafficLightSimulation: OnUpdate @ node {NodeId}: Adding live traffic lights to segment {segmentId}");
#endif

                // add custom lights
                if (!TrafficLight.CustomTrafficLights.IsSegmentLight(NodeId, segmentId))
                {
                    TrafficLight.CustomTrafficLights.AddSegmentLights(NodeId, segmentId);
                }

                // housekeep timed light
                TrafficLight.CustomTrafficLights.GetSegmentLights(NodeId, segmentId).housekeeping(true);
            }

            // ensure there is a physical traffic light
            Flags.setNodeTrafficLight(NodeId, true);

            TimedLight?.handleNewSegments();
            TimedLight?.housekeeping();
        }
Exemplo n.º 3
0
        public void SimulationStep()
        {
            var currentFrame = Singleton <SimulationManager> .instance.m_currentFrameIndex >> 5;

#if DEBUGTTL
            Log._Debug($"TTL SimStep: currentFrame={currentFrame} lastSimulationStep={lastSimulationStep}");
#endif
            if (lastSimulationStep >= currentFrame)
            {
#if DEBUGTTL
                Log._Debug($"TTL SimStep: *STOP* lastSimulationStep >= currentFrame");
#endif
                return;
            }
            lastSimulationStep = currentFrame;

            if (!isMasterNode() || !IsStarted())
            {
#if DEBUGTTL
                Log._Debug($"TTL SimStep: *STOP* isMasterNode={isMasterNode()} IsStarted={IsStarted()}");
#endif
                return;
            }
            if (!housekeeping(false))
            {
#if DEBUGTTL
                Log.Warning($"TTL SimStep: *STOP* Housekeeping detected that this timed traffic light has become invalid: {NodeId}.");
#endif
                Stop();
                return;
            }

            if (!Steps[CurrentStep].isValid())
            {
#if DEBUGTTL
                Log._Debug($"TTL SimStep: *STOP* current step ({CurrentStep}) is not valid.");
#endif
                TrafficLightSimulation.RemoveNodeFromSimulation(NodeId, false);
                return;
            }

#if DEBUGTTL
            Log._Debug($"TTL SimStep: Setting lights (1)");
#endif
            SetLights();

            if (!Steps[CurrentStep].StepDone(true))
            {
#if DEBUGTTL
                Log._Debug($"TTL SimStep: *STOP* current step ({CurrentStep}) is not done.");
#endif
                return;
            }
            // step is done

#if DEBUGTTL
            Log._Debug($"TTL SimStep: Setting lights (2)");
#endif
            SetLights();

            if (!Steps[CurrentStep].isEndTransitionDone())
            {
#if DEBUGTTL
                Log._Debug($"TTL SimStep: *STOP* current step ({CurrentStep}): end transition is not done.");
#endif
                return;
            }
            // ending transition (yellow) finished

#if DEBUGTTL
            Log._Debug($"TTL SimStep: ending transition done. NodeGroup={string.Join(", ", NodeGroup.Select(x => x.ToString()).ToArray())}, nodeId={NodeId}, NumSteps={NumSteps()}");
#endif

            // change step
            var newCurrentStep = (CurrentStep + 1) % NumSteps();
            foreach (ushort slaveNodeId in NodeGroup)
            {
                TrafficLightSimulation slaveSim = TrafficLightSimulation.GetNodeSimulation(slaveNodeId);
                if (slaveSim == null || !slaveSim.IsTimedLight())
                {
                    continue;
                }

                slaveSim.TimedLight.CurrentStep = newCurrentStep;
                slaveSim.TimedLight.Steps[newCurrentStep].Start();
                slaveSim.TimedLight.Steps[newCurrentStep].SetLights();
            }
        }
Exemplo n.º 4
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);
        }
        public void SimulationStep()
        {
#if TRACE
            Singleton <CodeProfiler> .instance.Start("TimedTrafficLights.SimulationStep");
#endif
            if (!isMasterNode() || !IsStarted())
            {
#if DEBUGTTL
                Log._Debug($"TTL SimStep: *STOP* NodeId={NodeId} isMasterNode={isMasterNode()} IsStarted={IsStarted()}");
#endif
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("TimedTrafficLights.SimulationStep");
#endif
                return;
            }

            var currentFrame = Singleton <SimulationManager> .instance.m_currentFrameIndex >> 5;
#if DEBUGTTL
            Log._Debug($"TTL SimStep: nodeId={NodeId} currentFrame={currentFrame} lastSimulationStep={lastSimulationStep}");
#endif
            if (lastSimulationStep >= currentFrame)
            {
#if DEBUGTTL
                Log._Debug($"TTL SimStep: *STOP* lastSimulationStep >= currentFrame");
#endif
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("TimedTrafficLights.SimulationStep");
#endif
                return;
            }
            lastSimulationStep = currentFrame;

            /*if (!housekeeping()) {
             #if DEBUGTTL
             *      Log.Warning($"TTL SimStep: *STOP* NodeId={NodeId} Housekeeping detected that this timed traffic light has become invalid: {NodeId}.");
             #endif
             *      Stop();
             *      return;
             * }*/

            if (!Steps[CurrentStep].isValid())
            {
#if DEBUGTTL
                Log._Debug($"TTL SimStep: *STOP* NodeId={NodeId} current step ({CurrentStep}) is not valid.");
#endif
                TrafficLightSimulation.RemoveNodeFromSimulation(NodeId, false, false);
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("TimedTrafficLights.SimulationStep");
#endif
                return;
            }

#if DEBUGTTL
            Log._Debug($"TTL SimStep: NodeId={NodeId} Setting lights (1)");
#endif
            SetLights();

            if (!Steps[CurrentStep].StepDone(true))
            {
#if DEBUGTTL
                Log._Debug($"TTL SimStep: *STOP* NodeId={NodeId} current step ({CurrentStep}) is not done.");
#endif
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("TimedTrafficLights.SimulationStep");
#endif
                return;
            }
            // step is done

#if DEBUGTTL
            Log._Debug($"TTL SimStep: NodeId={NodeId} Setting lights (2)");
#endif
            SetLights();             // check if this is needed

            if (!Steps[CurrentStep].isEndTransitionDone())
            {
#if DEBUGTTL
                Log._Debug($"TTL SimStep: *STOP* NodeId={NodeId} current step ({CurrentStep}): end transition is not done.");
#endif
#if TRACE
                Singleton <CodeProfiler> .instance.Stop("TimedTrafficLights.SimulationStep");
#endif
                return;
            }
            // ending transition (yellow) finished

#if DEBUGTTL
            Log._Debug($"TTL SimStep: NodeId={NodeId} ending transition done. NodeGroup={string.Join(", ", NodeGroup.Select(x => x.ToString()).ToArray())}, nodeId={NodeId}, NumSteps={NumSteps()}");
#endif

            // change step
            int oldCurrentStep = CurrentStep;
            while (true)
            {
                SkipStep(false);

                if (CurrentStep == oldCurrentStep)
                {
                    break;
                }

                if (Steps[CurrentStep].minTime > 0)
                {
                    break;
                }

                float flow, wait;
                bool  couldCalculateFlowWaitStats = Steps[CurrentStep].calcWaitFlow(out wait, out flow);

                if (!couldCalculateFlowWaitStats)
                {
                    break;
                }

                if (flow >= wait)
                {
                    break;
                }
            }
            Steps[CurrentStep].SetLights();
#if TRACE
            Singleton <CodeProfiler> .instance.Stop("TimedTrafficLights.SimulationStep");
#endif
        }