Inheritance: RoadBaseAI
Exemplo n.º 1
0
        public TrafficLightsTimed(ushort nodeID, List <ushort> nodeGroup)
        {
            this.nodeID    = nodeID;
            this.nodeGroup = new List <ushort>(nodeGroup);

            CustomRoadAI.GetNodeSimulation(nodeID).TimedTrafficLightsActive = false;
        }
Exemplo n.º 2
0
        public void start()
        {
            uint currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex;

            currentStep = 0;
            steps[0].setLights();
            steps[0].setFrame(currentFrameIndex >> 6);

            CustomRoadAI.GetNodeSimulation(nodeID).TimedTrafficLightsActive = true;
        }
Exemplo n.º 3
0
        public bool CheckTrafficLights(ushort node, ushort segment)
        {
            var nodeSimulation = CustomRoadAI.GetNodeSimulation(node);

            NetManager instance          = Singleton <NetManager> .instance;
            uint       currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex;
            uint       num  = (uint)(((int)node << 8) / 32768);
            uint       num2 = currentFrameIndex - num & 255u;

            RoadBaseAI.TrafficLightState vehicleLightState;
            RoadBaseAI.TrafficLightState pedestrianLightState;
            bool vehicles;
            bool flag;

            if (nodeSimulation == null || (nodeSimulation.FlagTimedTrafficLights && !nodeSimulation.TimedTrafficLightsActive))
            {
                RoadBaseAI.GetTrafficLightState(node, ref instance.m_segments.m_buffer[(int)segment],
                                                currentFrameIndex - num, out vehicleLightState, out pedestrianLightState, out vehicles, out flag);
                switch (pedestrianLightState)
                {
                case RoadBaseAI.TrafficLightState.RedToGreen:
                    if (num2 < 60u)
                    {
                        return(false);
                    }
                    break;

                case RoadBaseAI.TrafficLightState.Red:
                case RoadBaseAI.TrafficLightState.GreenToRed:
                    if (!flag && num2 >= 196u)
                    {
                        flag = true;
                        RoadBaseAI.SetTrafficLightState(node, ref instance.m_segments.m_buffer[(int)segment],
                                                        currentFrameIndex - num, vehicleLightState, pedestrianLightState, vehicles, flag);
                    }
                    return(false);
                }
                return(true);
            }
            else
            {
                if (TrafficLightsManual.IsSegmentLight(node, segment) && TrafficLightsManual.GetSegmentLight(node, segment).GetLightPedestrian() ==
                    RoadBaseAI.TrafficLightState.Red)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
Exemplo n.º 4
0
        public void Update()
        {
            uint currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex >> 6;

            if (lastFrame < currentFrameIndex)
            {
                lastFrame = currentFrameIndex;

                List <ushort> clearedNodes = new List <ushort>();

                foreach (var nodeID in nodeDictionary.Keys)
                {
                    var data = TrafficLightTool.GetNetNode(nodeID);

                    try
                    {
                        for (var i = 0; i < 8; i++)
                        {
                            var sgmid = data.GetSegment(i);

                            if (sgmid != 0)
                            {
                                if (!TrafficLightsManual.IsSegmentLight(nodeID, sgmid))
                                {
                                    if (nodeDictionary[nodeID].FlagTimedTrafficLights)
                                    {
                                        // TODO: this fails after an upgrade
                                        var timedNode = TrafficLightsTimed.GetTimedLight(nodeID);

                                        for (var j = 0; j < timedNode.nodeGroup.Count; j++)
                                        {
                                            var nodeSim = CustomRoadAI.GetNodeSimulation(timedNode.nodeGroup[j]);

                                            nodeSim.TimedTrafficLightsActive = false;

                                            clearedNodes.Add(timedNode.nodeGroup[j]);
                                            TrafficLightsTimed.RemoveTimedLight(timedNode.nodeGroup[j]);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.Log(e);
                        //Log.Warning("Error on Update: \n" + e.Message + "\n\nStacktrace:\n\n" + e.StackTrace);
                    }
                }

                if (clearedNodes.Count > 0)
                {
                    for (var i = 0; i < clearedNodes.Count; i++)
                    {
                        CustomRoadAI.RemoveNodeFromSimulation(clearedNodes[i]);
                    }
                }

                foreach (var nodeID in nodeDictionary.Keys)
                {
                    var node = GetNodeSimulation(nodeID);

                    if (node.FlagManualTrafficLights || (node.FlagTimedTrafficLights && node.TimedTrafficLightsActive))
                    {
                        var data = TrafficLightTool.GetNetNode(nodeID);

                        node.SimulationStep(ref data);
                        TrafficLightTool.SetNetNode(nodeID, data);

                        if (clearedNodes.Count > 0)
                        {
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 5
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;
        }
Exemplo n.º 6
0
 public bool isStarted()
 {
     return(CustomRoadAI.GetNodeSimulation(nodeID).TimedTrafficLightsActive);
 }
Exemplo n.º 7
0
 public void stop()
 {
     CustomRoadAI.GetNodeSimulation(nodeID).TimedTrafficLightsActive = false;
 }