Ejemplo n.º 1
0
        public void ChangeMode()
        {
            var hasLeftSegment    = TrafficPriority.HasLeftSegment(this.segment, this.node) && TrafficPriority.hasLeftLane(this.node, this.segment);
            var hasForwardSegment = TrafficPriority.HasForwardSegment(this.segment, this.node) && TrafficPriority.hasForwardLane(this.node, this.segment);
            var hasRightSegment   = TrafficPriority.HasRightSegment(this.segment, this.node) && TrafficPriority.hasRightLane(this.node, this.segment);

            if (currentMode == ManualSegmentLight.Mode.Simple)
            {
                if (!hasLeftSegment)
                {
                    currentMode = ManualSegmentLight.Mode.RightForwardL;
                }
                else
                {
                    currentMode = ManualSegmentLight.Mode.LeftForwardR;
                }
            }
            else if (currentMode == ManualSegmentLight.Mode.LeftForwardR)
            {
                if (!hasForwardSegment || !hasRightSegment)
                {
                    currentMode = ManualSegmentLight.Mode.Simple;
                }
                else
                {
                    currentMode = ManualSegmentLight.Mode.RightForwardL;
                }
            }
            else if (currentMode == ManualSegmentLight.Mode.RightForwardL)
            {
                if (!hasLeftSegment)
                {
                    currentMode = ManualSegmentLight.Mode.Simple;
                }
                else
                {
                    currentMode = ManualSegmentLight.Mode.All;
                }
            }
            else
            {
                currentMode = ManualSegmentLight.Mode.Simple;
            }

            if (currentMode == Mode.Simple)
            {
                lightLeft       = lightMain;
                lightRight      = lightMain;
                lightPedestrian = _checkPedestrianLight();
            }
        }
Ejemplo n.º 2
0
        public static void OnLoadDataTimed(System.Object source, ElapsedEventArgs e)
        {
            _timer.Enabled = false;
            byte[] data = SerializableData.LoadData(dataID);

            uniqueID = 0u;
            var i = 0;

            for (i = 0; i < data.Length - 3; i++)
            {
                uniqueID = BitConverter.ToUInt32(data, i);
            }

            var filepath = Path.Combine(Application.dataPath, "trafficManagerSave_" + uniqueID + ".xml");

            if (!File.Exists(filepath))
            {
                Debug.Log("Traffic manager save file " + filepath + " not found!");
                return;
            }

            var configuration = Configuration.Deserialize(filepath);

            try {
                for (i = 0; i < configuration.prioritySegments.Count; i++)
                {
                    if (!TrafficPriority.isPrioritySegment((ushort)configuration.prioritySegments[i][0],
                                                           configuration.prioritySegments[i][1]))
                    {
                        TrafficPriority.addPrioritySegment((ushort)configuration.prioritySegments[i][0],
                                                           configuration.prioritySegments[i][1],
                                                           (PrioritySegment.PriorityType)configuration.prioritySegments[i][2]);
                    }
                }
            } catch (Exception ex) {
                Debug.Log("prio segments exception at " + i + " - " + ex);
            }


            try {
                for (i = 0; i < configuration.nodeDictionary.Count; i++)
                {
                    if (CustomRoadAI.GetNodeSimulation((ushort)configuration.nodeDictionary[i][0]) == null)
                    {
                        CustomRoadAI.AddNodeToSimulation((ushort)configuration.nodeDictionary[i][0]);
                        var nodeDict = CustomRoadAI.GetNodeSimulation((ushort)configuration.nodeDictionary[i][0]);

                        nodeDict._manualTrafficLights     = Convert.ToBoolean(configuration.nodeDictionary[i][1]);
                        nodeDict._timedTrafficLights      = Convert.ToBoolean(configuration.nodeDictionary[i][2]);
                        nodeDict.TimedTrafficLightsActive = Convert.ToBoolean(configuration.nodeDictionary[i][3]);
                    }
                }
            } catch (Exception ex) {
                Debug.Log("nodes exception at " + i + " - " + ex);
            }

            try {
                for (i = 0; i < configuration.manualSegments.Count; i++)
                {
                    var segmentData = configuration.manualSegments[i];

                    if (!TrafficLightsManual.IsSegmentLight((ushort)segmentData[0], segmentData[1]))
                    {
                        TrafficLightsManual.AddSegmentLight((ushort)segmentData[0], segmentData[1],
                                                            RoadBaseAI.TrafficLightState.Green);
                        var segment = TrafficLightsManual.GetSegmentLight((ushort)segmentData[0], segmentData[1]);
                        segment.currentMode       = (ManualSegmentLight.Mode)segmentData[2];
                        segment.lightLeft         = (RoadBaseAI.TrafficLightState)segmentData[3];
                        segment.lightMain         = (RoadBaseAI.TrafficLightState)segmentData[4];
                        segment.lightRight        = (RoadBaseAI.TrafficLightState)segmentData[5];
                        segment.lightPedestrian   = (RoadBaseAI.TrafficLightState)segmentData[6];
                        segment.lastChange        = (uint)segmentData[7];
                        segment.lastChangeFrame   = (uint)segmentData[8];
                        segment.pedestrianEnabled = Convert.ToBoolean(segmentData[9]);
                    }
                }
            } catch (Exception ex) {
                Debug.Log("traf lights manual exception at " + i + " - " + ex);
            }

            var timedStepCount        = 0;
            var timedStepSegmentCount = 0;

            try {
                for (i = 0; i < configuration.timedNodes.Count; i++)
                {
                    var nodeid = (ushort)configuration.timedNodes[i][0];

                    var nodeGroup = new List <ushort>();
                    for (var j = 0; j < configuration.timedNodeGroups[i].Length; j++)
                    {
                        nodeGroup.Add(configuration.timedNodeGroups[i][j]);
                    }

                    if (!TrafficLightsTimed.IsTimedLight(nodeid))
                    {
                        TrafficLightsTimed.AddTimedLight(nodeid, nodeGroup);
                        var timedNode = TrafficLightsTimed.GetTimedLight(nodeid);

                        timedNode.currentStep = configuration.timedNodes[i][1];

                        for (var j = 0; j < configuration.timedNodes[i][2]; j++)
                        {
                            var cfgstep = configuration.timedNodeSteps[timedStepCount];

                            timedNode.addStep(cfgstep[0]);

                            var step = timedNode.steps[j];

                            for (var k = 0; k < cfgstep[1]; k++)
                            {
                                step.lightLeft[k]       = (RoadBaseAI.TrafficLightState)configuration.timedNodeStepSegments[timedStepSegmentCount][0];
                                step.lightMain[k]       = (RoadBaseAI.TrafficLightState)configuration.timedNodeStepSegments[timedStepSegmentCount][1];
                                step.lightRight[k]      = (RoadBaseAI.TrafficLightState)configuration.timedNodeStepSegments[timedStepSegmentCount][2];
                                step.lightPedestrian[k] = (RoadBaseAI.TrafficLightState)configuration.timedNodeStepSegments[timedStepSegmentCount][3];

                                timedStepSegmentCount++;
                            }

                            timedStepCount++;
                        }

                        if (Convert.ToBoolean(configuration.timedNodes[i][3]))
                        {
                            timedNode.start();
                        }
                    }
                }
            } catch (Exception ex) {
                Debug.Log("timed traf lights exception at " + i + " - " + ex);
            }


            var j1 = 0;
            var i1 = 0;

            try {
                for (i1 = 0; i1 < 32768; i1++)
                {
                    if (Singleton <NetManager> .instance.m_nodes.m_buffer[i1].Info.m_class.m_service ==
                        ItemClass.Service.Road && Singleton <NetManager> .instance.m_nodes.m_buffer[i1].m_flags != 0)
                    {
                        var trafficLight = configuration.nodeTrafficLights[j1];

                        if (trafficLight == '1')
                        {
                            Singleton <NetManager> .instance.m_nodes.m_buffer[i1].m_flags |= NetNode.Flags.TrafficLights;
                        }
                        else
                        {
                            Singleton <NetManager> .instance.m_nodes.m_buffer[i1].m_flags &= ~NetNode.Flags.TrafficLights;
                        }

                        j1++;
                    }
                }
            } catch (Exception ex) {
                Debug.Log("traf lights exception at i1 = " + i1 + " j1 = " + j1 + " - " + ex);
            }

            var j2 = 0;
            var i2 = 0;

            try {
                for (i2 = 0; i2 < 32768; i2++)
                {
                    if (Singleton <NetManager> .instance.m_nodes.m_buffer[i2].Info.m_class.m_service ==
                        ItemClass.Service.Road && Singleton <NetManager> .instance.m_nodes.m_buffer[i2].m_flags != 0)
                    {
                        var crossWalk = configuration.nodeCrosswalk[j2];

                        if (crossWalk == '1')
                        {
                            Singleton <NetManager> .instance.m_nodes.m_buffer[i2].m_flags |= NetNode.Flags.Junction;
                        }
                        else
                        {
                            Singleton <NetManager> .instance.m_nodes.m_buffer[i2].m_flags &= ~NetNode.Flags.Junction;
                        }

                        j2++;
                    }
                }
            } catch (Exception ex) {
                Debug.Log("crosswalk exception at i2 = " + i2 + " j2 = " + j2 + " - " + ex);
            }

            var lanes = configuration.laneFlags.TrimEnd(',').Split(',');

            try {
                for (i = 0; i < lanes.Length; i++)
                {
                    var  split  = lanes[i].Split(':');
                    uint laneId = Convert.ToUInt32(split[0]);
                    //NetLane lane = Singleton<NetManager>.instance.m_lanes.m_buffer [laneId];
                    //ushort segmentId = lane.m_segment;
                    //NetSegment segment = Singleton<NetManager>.instance.m_segments.m_buffer [segmentId];
                    //segment.Info.m_netAI.UpdateLanes(segmentId, ref segment, false);

                    Singleton <NetManager> .instance.m_lanes.m_buffer[laneId].m_flags  = Convert.ToUInt16(split[1]);
                    Singleton <NetManager> .instance.m_lanes.m_buffer[laneId].m_flags |= CONTROL_BIT;
                }
            } catch {
                // Empty config, ignore exception.
                //Debug.Log("exception reading lane flags at lane " + i + " lanes[] = [" + lanes[i] + "] - " + ex);
            }

            if (configuration.aiConfig != null && configuration.aiConfig.congestionCostFactor > 0)
            {
                Debug.Log("setting AI values from config");
                CustomPathFind.LoadAIParameters(configuration.aiConfig);
            }
            else
            {
                Debug.Log("AI parameters not found in config, using default values");
                CustomPathFind.ResetAIParameters();
            }

            CSL_Traffic.RoadManager.sm_lanes = new CSL_Traffic.RoadManager.Lane[NetManager.MAX_LANE_COUNT];
            int dupes = 0, zero = 0;

            if (configuration.laneMarkers == null || configuration.laneMarkers.Count == 0)
            {
                Debug.Log("no lane markers found");
            }
            else
            {
                Debug.Log("found " + configuration.laneMarkers.Count + " lane markers");
                foreach (var lane in configuration.laneMarkers.ToArray())
                {
                    if (lane != null)
                    {
                        if (CSL_Traffic.RoadManager.sm_lanes[lane.m_laneId] == null)
                        {
                            if (lane.ConnectionCount() > 0)
                            {
                                CSL_Traffic.RoadManager.sm_lanes[lane.m_laneId] = lane;
                            }
                            else
                            {
                                zero++;
                            }
                        }
                        else
                        {
                            dupes++;
                        }
                    }
                    else
                    {
                        Debug.Log("null lane marker!");
                    }
                }
                Debug.Log("loaded lane markers - " + dupes + " duplicates, " + zero + " unconnected");
            }

            configLoaded = true;
        }