Example #1
0
        public void SimulationStep(ref NetNode data)
        {
            NetManager instance          = Singleton <NetManager> .instance;
            uint       currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex;

            if (TrafficLightsTimed.IsTimedLight(NodeId) && TimedTrafficLightsActive)
            {
                var timedNode = TrafficLightsTimed.GetTimedLight(NodeId);
                timedNode.checkStep(currentFrameIndex >> 6);
            }

            for (int l = 0; l < 8; l++)
            {
                ushort segment = data.GetSegment(l);
                if (segment != 0)
                {
                    if (TrafficLightsManual.IsSegmentLight(NodeId, segment))
                    {
                        var segmentLight = TrafficLightsManual.GetSegmentLight(NodeId, segment);

                        segmentLight.lastChange = (currentFrameIndex >> 6) - segmentLight.lastChangeFrame;
                    }
                }
            }
        }
Example #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;
        }
Example #3
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;
                        }
                    }
                }
            }
        }
Example #4
0
        public void OnSaveData()
        {
            FastList <byte> data = new FastList <byte>();

//            Debug.Log("OnSaveData() 1");
            GenerateUniqueID();

            byte[] uniqueIdBytes = BitConverter.GetBytes(uniqueID);
            foreach (byte uniqueIdByte in uniqueIdBytes)
            {
                data.Add(uniqueIdByte);
            }
//            Debug.Log("OnSaveData() 2");

            byte[] dataToSave = data.ToArray();
            SerializableData.SaveData(dataID, dataToSave);

//            Debug.Log("OnSaveData() 3");
            var filepath = Path.Combine(Application.dataPath, "trafficManagerSave_" + uniqueID + ".xml");
//            Debug.Log("OnSaveData()");

            var configuration = new Configuration();

//            Debug.Log("OnSaveData() 4");

            configuration.laneFlags         = "";
            configuration.nodeCrosswalk     = "";
            configuration.nodeTrafficLights = "";

            for (var i = 0; i < 32768; i++)
            {
                if (TrafficPriority.prioritySegments.ContainsKey(i))
                {
                    if (TrafficPriority.prioritySegments[i].node_1 != 0)
                    {
                        configuration.prioritySegments.Add(new int[3] {
                            TrafficPriority.prioritySegments[i].node_1,
                            i,
                            (int)TrafficPriority.prioritySegments[i].instance_1.type
                        });
                    }
                    //Debug.Log("OnSaveData() 5");

                    if (TrafficPriority.prioritySegments[i].node_2 != 0)
                    {
                        configuration.prioritySegments.Add(new int[3] {
                            TrafficPriority.prioritySegments[i].node_2,
                            i,
                            (int)TrafficPriority.prioritySegments[i].instance_2.type
                        });
                    }
                }
                //Debug.Log("OnSaveData() 6");

                if (CustomRoadAI.nodeDictionary.ContainsKey((ushort)i))
                {
                    var nodeDict = CustomRoadAI.nodeDictionary[(ushort)i];

                    configuration.nodeDictionary.Add(new int[4] {
                        nodeDict.NodeId,
                        Convert.ToInt32(nodeDict._manualTrafficLights),
                        Convert.ToInt32(nodeDict._timedTrafficLights),
                        Convert.ToInt32(nodeDict.TimedTrafficLightsActive)
                    });
                }
//                Debug.Log("OnSaveData() 7");

                if (TrafficLightsManual.ManualSegments.ContainsKey(i))
                {
                    if (TrafficLightsManual.ManualSegments[i].node_1 != 0)
                    {
                        var manualSegment = TrafficLightsManual.ManualSegments[i].instance_1;

                        configuration.manualSegments.Add(new int[10] {
                            (int)manualSegment.node,
                            manualSegment.segment,
                            (int)manualSegment.currentMode,
                            (int)manualSegment.lightLeft,
                            (int)manualSegment.lightMain,
                            (int)manualSegment.lightRight,
                            (int)manualSegment.lightPedestrian,
                            (int)manualSegment.lastChange,
                            (int)manualSegment.lastChangeFrame,
                            Convert.ToInt32(manualSegment.pedestrianEnabled)
                        });
                    }
                    //Debug.Log("OnSaveData() 8");

                    if (TrafficLightsManual.ManualSegments[i].node_2 != 0)
                    {
                        var manualSegment = TrafficLightsManual.ManualSegments[i].instance_2;
                        //Debug.Log("OnSaveData() 9");

                        configuration.manualSegments.Add(new int[10] {
                            (int)manualSegment.node,
                            manualSegment.segment,
                            (int)manualSegment.currentMode,
                            (int)manualSegment.lightLeft,
                            (int)manualSegment.lightMain,
                            (int)manualSegment.lightRight,
                            (int)manualSegment.lightPedestrian,
                            (int)manualSegment.lastChange,
                            (int)manualSegment.lastChangeFrame,
                            Convert.ToInt32(manualSegment.pedestrianEnabled)
                        });
                    }
                }
//                Debug.Log("OnSaveData() 10");

                if (TrafficLightsTimed.timedScripts.ContainsKey((ushort)i))
                {
                    var timedNode = TrafficLightsTimed.GetTimedLight((ushort)i);

                    configuration.timedNodes.Add(new int[4] {
                        timedNode.nodeID,
                        timedNode.currentStep,
                        timedNode.NumSteps(),
                        Convert.ToInt32(timedNode.isStarted())
                    });

                    var nodeGroup = new ushort[timedNode.nodeGroup.Count];

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

                    configuration.timedNodeGroups.Add(nodeGroup);

                    for (var j = 0; j < timedNode.NumSteps(); j++)
                    {
                        configuration.timedNodeSteps.Add(new int[2] {
                            timedNode.steps[j].numSteps,
                            timedNode.steps[j].segments.Count
                        });

                        for (var k = 0; k < timedNode.steps[j].segments.Count; k++)
                        {
                            configuration.timedNodeStepSegments.Add(new int[4] {
                                (int)timedNode.steps[j].lightLeft[k],
                                (int)timedNode.steps[j].lightMain[k],
                                (int)timedNode.steps[j].lightRight[k],
                                (int)timedNode.steps[j].lightPedestrian[k],
                            });
                        }
                    }
                }
            }
            //Debug.Log("OnSaveData() 11");

            for (var i = 0; i < Singleton <NetManager> .instance.m_nodes.m_buffer.Length; i++)
            {
                var nodeFlags = Singleton <NetManager> .instance.m_nodes.m_buffer[i].m_flags;

                if (nodeFlags != 0)
                {
                    if (Singleton <NetManager> .instance.m_nodes.m_buffer[i].Info.m_class.m_service ==
                        ItemClass.Service.Road)
                    {
                        configuration.nodeTrafficLights +=
                            Convert.ToInt16((nodeFlags & NetNode.Flags.TrafficLights) != NetNode.Flags.None);
                        configuration.nodeCrosswalk +=
                            Convert.ToInt16((nodeFlags & NetNode.Flags.Junction) != NetNode.Flags.None);
                    }
                }
            }
            //Debug.Log("OnSaveData() 12");

            var laneCount = 0;

            for (var i = 0; i < Singleton <NetManager> .instance.m_lanes.m_buffer.Length; i++)
            {
                var laneSegment = Singleton <NetManager> .instance.m_lanes.m_buffer[i].m_segment;

                if (TrafficPriority.prioritySegments.ContainsKey(laneSegment))
                {
                    configuration.laneFlags += i + ":" + Singleton <NetManager> .instance.m_lanes.m_buffer[i].m_flags + ",";
                    laneCount++;
                }
            }

            if (configuration.laneFlags != null && configuration.laneFlags.Length > 0)
            {
                configuration.laneFlags = configuration.laneFlags.TrimEnd(',');
            }

            configuration.aiConfig.congestionCostFactor   = CustomPathFind.congestionCostFactor;
            configuration.aiConfig.minLaneSpace           = CustomPathFind.minLaneSpace;
            configuration.aiConfig.lookaheadLanes         = CustomPathFind.lookaheadLanes;
            configuration.aiConfig.congestedLaneThreshold = CustomPathFind.congestedLaneThreshold;
            configuration.aiConfig.obeyTMLaneFlags        = CustomPathFind.obeyTMLaneFlags;

            for (var i = 0; i < CSL_Traffic.RoadManager.sm_lanes.Length; i++)
            {
                var lane = CSL_Traffic.RoadManager.sm_lanes[i];
                if (lane != null && lane.ConnectionCount() > 0)
                {
                    configuration.laneMarkers.Add(lane);
                }
            }

            Configuration.Serialize(filepath, configuration);
        }