private static void AdjustSharpTurns(int inLaneCount, ref JunctionInfo nodeInfo)
        {
            int connectableLaneCount = nodeInfo.laneCounts[Direction.Left] +
                                       nodeInfo.laneCounts[Direction.Forward] +
                                       nodeInfo.laneCounts[Direction.Right];

            int totalLaneCount = connectableLaneCount +
                                 nodeInfo.laneCounts[Direction.SharpLeft] +
                                 nodeInfo.laneCounts[Direction.SharpRight];

            // If the number of connectable lanes is lower than the number of incoming lanes, sharp left/right lanes re-assign some or all of them into "normal" left/right.
            while (inLaneCount > connectableLaneCount && connectableLaneCount != totalLaneCount)
            {
                if (nodeInfo.laneCounts[Direction.SharpLeft] >= nodeInfo.laneCounts[Direction.SharpRight])
                {
                    nodeInfo.laneCounts[Direction.SharpLeft] -= 1;
                    nodeInfo.laneCounts[Direction.Left]      += 1;
                }
                else
                {
                    nodeInfo.laneCounts[Direction.SharpRight] -= 1;
                    nodeInfo.laneCounts[Direction.Right]      += 1;
                }

                connectableLaneCount += 1;
            }
        }
Esempio n. 2
0
        public void PreProcess(ref LaneInfo inLanes, ref JunctionInfo nodeInfo)
        {
            int inLaneCount    = inLanes.GetLaneCount();
            int inBusLaneCount = inLanes.GetBusLaneCount();

            if (inLaneCount == inBusLaneCount)
            {
                //If all IN lanes are bus lanes, all IN and OUT lanes are treated as normal car lanes.
                return;
            }

            int outLaneCount    = nodeInfo.GetLaneCount();
            int outBusLaneCount = nodeInfo.GetBusLaneCount();

            if (outLaneCount == outBusLaneCount)
            {
                //If all OUT lanes are bus lanes, all IN and OUT lanes are also treated as normal car lanes.
                return;
            }

            // If the number of IN car lanes is greater than OUT car lanes, then OUT bus lanes are also treated as normal car lanes.
            bool applyOutLaneRules = (inLaneCount - inBusLaneCount) <= (outLaneCount - outBusLaneCount);

            // Pre-process IN lanes
            int i = 0;

            var posToRemove = new List <float>();

            foreach (var lane in inLanes.lanes)
            {
                if (inLanes.busLaneIds.Contains(lane.Value))
                {
                    inBusLanes.Add(lane.Key, lane.Value);
                    inBusLaneIndices.Add(i);

                    inLanes.busLaneIds.Remove(lane.Value);

                    posToRemove.Add(lane.Key);
                }
                i += 1;
            }

            foreach (var pos in posToRemove)
            {
                inLanes.lanes.Remove(pos);
            }

            // Pre-process OUT lanes
            if (applyOutLaneRules)
            {
                outBusLaneIndices    = nodeInfo.GetBusLaneIndices();
                outBusLaneDirections = nodeInfo.RemoveLanes(outBusLaneIndices);
            }
        }
        // Adapted from NetNode.CountLanes
        private static JunctionInfo AnalyseNode(NetNode node,
                                                ushort nodeID,
                                                ushort ignoreSegmentID,
                                                NetInfo.LaneType laneTypes,
                                                VehicleInfo.VehicleType vehicleTypes,
                                                Vector3 directionVector)
        {
            var nodeInfo = new JunctionInfo();

            if (node.m_flags == NetNode.Flags.None)
            {
                return(nodeInfo);
            }

            NetManager netManager = Singleton <NetManager> .instance;

            for (int i = 0; i < 8; i++)
            {
                ushort segmentID = node.GetSegment(i);
                if (segmentID == 0 || segmentID == ignoreSegmentID)
                {
                    continue;
                }

                var segment = netManager.m_segments.m_buffer[segmentID];

                if (segment.Info == null)
                {
                    continue; // may happen if loading a map with a missing road asset
                }

                bool isInverted  = (segment.m_flags & NetSegment.Flags.Invert) != 0;
                bool isStartNode = segment.m_startNode == nodeID;

                SegmentLanes lanes = SegmentAnalyser.IdentifyLanes(netManager, segment.Info, segmentID);

                Vector3 vector = isStartNode ? segment.m_startDirection : segment.m_endDirection;

                float angleDeg = GetAngleDeg(vector, directionVector);

                nodeInfo.AddLanes(isStartNode ^ isInverted ? lanes.forward : lanes.backward, angleDeg);
            }

            return(nodeInfo);
        }