Beispiel #1
0
        /// <summary>
        /// Creates and configures default traffic the input junction
        /// </summary>
        /// <param name="nodeId">input junction</param>
        /// <returns>true if successful</returns>
        public static ErrorResult Setup(ushort nodeId)
        {
            if (tlsMan.HasTimedSimulation(nodeId))
            {
                return(ErrorResult.TTLExists);
            }

            // issue #575: Support level crossings.
            NetNode.Flags flags = Singleton <NetManager> .instance.m_nodes.m_buffer[nodeId].m_flags;
            if ((flags & NetNode.Flags.LevelCrossing) != 0)
            {
                return(ErrorResult.NotSupported);
            }

            var segList = ArrangedSegments(nodeId);
            int n       = segList.Count;

            if (n < 3)
            {
                return(ErrorResult.NotSupported);
            }

            if (!Add(nodeId))
            {
                return(ErrorResult.Other);
            }

            if (SeparateLanes)
            {
                SeparateTurningLanesUtil.SeparateNode(nodeId, out _);
            }

            //Is it special case:
            {
                var segList2Way = TwoWayRoads(segList, out int n2);
                if (n2 < 2)
                {
                    return(ErrorResult.NotSupported);
                }
                bool b = HasIncommingOneWaySegment(nodeId);
                if (n2 == 2 && !b)
                {
                    return(SetupSpecial(nodeId, segList2Way));
                }
            }

            for (int i = 0; i < n; ++i)
            {
                ITimedTrafficLightsStep step = TimedLight(nodeId).AddStep(
                    minTime: 3,
                    maxTime: 8,
                    changeMetric: StepChangeMetric.Default,
                    waitFlowBalance: 0.3f,
                    makeRed: true);

                SetupHelper(step, nodeId, segList[i], GreenDir.AllGreen);

                ushort nextSegmentId = segList[(i + 1) % n];
                if (NeedsShortOnly(nextSegmentId, nodeId))
                {
                    SetupHelper(step, nodeId, nextSegmentId, GreenDir.ShortOnly);
                }
                else
                {
                    SetupHelper(step, nodeId, nextSegmentId, GreenDir.AllRed);
                }
                for (int j = 2; j < n; ++j)
                {
                    SetupHelper(step, nodeId, segList[(i + j) % n], GreenDir.AllRed);
                }
            }

            Sim(nodeId).Housekeeping();
            TimedLight(nodeId).Start();
            return(ErrorResult.Success);
        }