/// <summary> /// Configures traffic light for and for all lane types at input segmentId, nodeId, and step. /// </summary> /// <param name="step"></param> /// <param name="nodeId"></param> /// <param name="segmentId"></param> /// <param name="m">Determines which directions are green</param> private static void SetupHelper(ITimedTrafficLightsStep step, ushort nodeId, ushort segmentId, GreenDir m) { bool startNode = (bool)netService.IsStartNode(segmentId, nodeId); //get step data for side seg ICustomSegmentLights liveSegmentLights = customTrafficLightsManager.GetSegmentLights(segmentId, startNode); //for each lane type foreach (ExtVehicleType vehicleType in liveSegmentLights.VehicleTypes) { //set light mode ICustomSegmentLight liveSegmentLight = liveSegmentLights.GetCustomLight(vehicleType); liveSegmentLight.CurrentMode = LightMode.All; TimedLight(nodeId).ChangeLightMode( segmentId, vehicleType, liveSegmentLight.CurrentMode); // set light states var green = RoadBaseAI.TrafficLightState.Green; var red = RoadBaseAI.TrafficLightState.Red; switch (m) { case GreenDir.AllRed: liveSegmentLight.SetStates(red, red, red); break; case GreenDir.AllGreen: liveSegmentLight.SetStates(green, green, green); break; case GreenDir.ShortOnly: { // calculate directions ref ExtSegmentEnd segEnd = ref segEndMan.ExtSegmentEnds[segEndMan.GetIndex(segmentId, nodeId)]; ref NetNode node = ref Singleton <NetManager> .instance.m_nodes.m_buffer[nodeId]; segEndMan.CalculateOutgoingLeftStraightRightSegments(ref segEnd, ref node, out bool bLeft, out bool bForward, out bool bRight); bool bShort = RHT ? bRight : bLeft; bool bLong = RHT ? bLeft : bRight; if (bShort) { SetStates(liveSegmentLight, red, red, green); } else if (bLong) { // go forward instead of short SetStates(liveSegmentLight, green, red, red); } else { Debug.LogAssertion("Unreachable code."); liveSegmentLight.SetStates(green, green, green); } break; }
/// <summary> /// speical case where: /// multiple outgoing one way roads. only two 2way roads. /// - each 1-way road gets a go /// - then the two 2-way roads get a go. /// this way we can save one step. /// </summary> /// <param name="nodeId"></param> /// <param name="segList2Way"></param> /// <returns></returns> private static ErrorResult SetupSpecial(ushort nodeId, List <ushort> segList2Way) { var segList1Way = OneWayRoads(nodeId, out var n1); // the two 2-way roads get a go. { ITimedTrafficLightsStep step = TimedLight(nodeId).AddStep( minTime: 3, maxTime: 8, changeMetric: StepChangeMetric.Default, waitFlowBalance: 0.3f, makeRed: true); SetupHelper(step, nodeId, segList2Way[0], GreenDir.AllGreen); SetupHelper(step, nodeId, segList2Way[1], GreenDir.AllGreen); foreach (var segId in segList1Way) { SetupHelper(step, nodeId, segId, GreenDir.AllRed); } } //each 1-way road gets a go for (int i = 0; i < n1; ++i) { ITimedTrafficLightsStep step = TimedLight(nodeId).AddStep( minTime: 3, maxTime: 8, changeMetric: StepChangeMetric.Default, waitFlowBalance: 0.3f, makeRed: true); SetupHelper(step, nodeId, segList1Way[i], GreenDir.AllGreen); for (int j = 1; j < n1; ++j) { SetupHelper(step, nodeId, segList1Way[(i + j) % n1], GreenDir.AllRed); } foreach (var segId in segList2Way) { SetupHelper(step, nodeId, segId, GreenDir.AllRed); } } Sim(nodeId).Housekeeping(); TimedLight(nodeId).Start(); return(ErrorResult.Success); }
public List <Configuration.TimedTrafficLights> SaveData(ref bool success) { var ret = new List <Configuration.TimedTrafficLights>(); for (uint nodeId = 0; nodeId < NetManager.MAX_NODE_COUNT; ++nodeId) { try { if (!TrafficLightSimulations[nodeId].IsTimedLight()) { continue; } #if DEBUGSAVE Log._Debug($"Going to save timed light at node {nodeId}."); #endif ITimedTrafficLights timedNode = TrafficLightSimulations[nodeId].timedLight; timedNode.OnGeometryUpdate(); var cnfTimedLights = new Configuration.TimedTrafficLights { nodeId = timedNode.NodeId, nodeGroup = new List <ushort>(timedNode.NodeGroup), started = timedNode.IsStarted() }; ret.Add(cnfTimedLights); int stepIndex = timedNode.CurrentStep; if (timedNode.IsStarted() && timedNode.GetStep(timedNode.CurrentStep).IsInEndTransition()) { // if in end transition save the next step stepIndex = (stepIndex + 1) % timedNode.NumSteps(); } cnfTimedLights.currentStep = stepIndex; cnfTimedLights.timedSteps = new List <Configuration.TimedTrafficLightsStep>(); for (var j = 0; j < timedNode.NumSteps(); j++) { #if DEBUGSAVE Log._Debug($"Saving timed light step {j} at node {nodeId}."); #endif ITimedTrafficLightsStep timedStep = timedNode.GetStep(j); var cnfTimedStep = new Configuration.TimedTrafficLightsStep { minTime = timedStep.MinTime, maxTime = timedStep.MaxTime, changeMetric = (int)timedStep.ChangeMetric, waitFlowBalance = timedStep.WaitFlowBalance, segmentLights = new Dictionary <ushort, Configuration.CustomSegmentLights>() }; cnfTimedLights.timedSteps.Add(cnfTimedStep); foreach (KeyValuePair <ushort, ICustomSegmentLights> e in timedStep.CustomSegmentLights) { #if DEBUGSAVE Log._Debug($"Saving timed light step {j}, segment {e.Key} at node {nodeId}."); #endif ICustomSegmentLights segLights = e.Value; ushort lightsNodeId = segLights.NodeId; var cnfSegLights = new Configuration.CustomSegmentLights { nodeId = lightsNodeId, // TODO not needed segmentId = segLights.SegmentId, // TODO not needed customLights = new Dictionary <ExtVehicleType, Configuration.CustomSegmentLight>(), pedestrianLightState = segLights.PedestrianLightState, manualPedestrianMode = segLights.ManualPedestrianMode }; if (lightsNodeId == 0 || lightsNodeId != timedNode.NodeId) { Log.Warning( "Inconsistency detected: Timed traffic light @ node " + $"{timedNode.NodeId} contains custom traffic lights for the invalid " + $"segment ({segLights.SegmentId}) at step {j}: nId={lightsNodeId}"); continue; } cnfTimedStep.segmentLights.Add(e.Key, cnfSegLights); #if DEBUGSAVE Log._Debug($"Saving pedestrian light @ seg. {e.Key}, step {j}: " + $"{cnfSegLights.pedestrianLightState} {cnfSegLights.manualPedestrianMode}"); #endif foreach (KeyValuePair <API.Traffic.Enums.ExtVehicleType, ICustomSegmentLight> e2 in segLights.CustomLights) { #if DEBUGSAVE Log._Debug($"Saving timed light step {j}, segment {e.Key}, vehicleType " + $"{e2.Key} at node {nodeId}."); #endif ICustomSegmentLight segLight = e2.Value; var cnfSegLight = new Configuration.CustomSegmentLight { nodeId = lightsNodeId, // TODO not needed segmentId = segLights.SegmentId, // TODO not needed currentMode = (int)segLight.CurrentMode, leftLight = segLight.LightLeft, mainLight = segLight.LightMain, rightLight = segLight.LightRight }; cnfSegLights.customLights.Add( LegacyExtVehicleType.ToOld(e2.Key), cnfSegLight); } } } } catch (Exception e) { Log.Error( $"Exception occurred while saving timed traffic light @ {nodeId}: {e}"); success = false; } } return(ret); }
public bool LoadData(List <Configuration.TimedTrafficLights> data) { bool success = true; Log.Info($"Loading {data.Count} timed traffic lights (new method)"); var nodesWithSimulation = new HashSet <ushort>(); foreach (Configuration.TimedTrafficLights cnfTimedLights in data) { nodesWithSimulation.Add(cnfTimedLights.nodeId); } var masterNodeIdBySlaveNodeId = new Dictionary <ushort, ushort>(); var nodeGroupByMasterNodeId = new Dictionary <ushort, List <ushort> >(); foreach (Configuration.TimedTrafficLights cnfTimedLights in data) { try { // TODO most of this should not be necessary at all if the classes around TimedTrafficLights class were properly designed // enforce uniqueness of node ids List <ushort> currentNodeGroup = cnfTimedLights.nodeGroup.Distinct().ToList(); if (!currentNodeGroup.Contains(cnfTimedLights.nodeId)) { currentNodeGroup.Add(cnfTimedLights.nodeId); } // remove any nodes that are not configured to have a simulation currentNodeGroup = new List <ushort>( currentNodeGroup.Intersect(nodesWithSimulation)); // remove invalid nodes from the group; find if any of the nodes in the group is already a master node ushort masterNodeId = 0; int foundMasterNodes = 0; for (int i = 0; i < currentNodeGroup.Count;) { ushort nodeId = currentNodeGroup[i]; if (!Services.NetService.IsNodeValid(currentNodeGroup[i])) { currentNodeGroup.RemoveAt(i); continue; } if (nodeGroupByMasterNodeId.ContainsKey(nodeId)) { // this is a known master node if (foundMasterNodes > 0) { // we already found another master node. ignore this node. currentNodeGroup.RemoveAt(i); continue; } // we found the first master node masterNodeId = nodeId; ++foundMasterNodes; } ++i; } if (masterNodeId == 0) { // no master node defined yet, set the first node as a master node masterNodeId = currentNodeGroup[0]; } // ensure the master node is the first node in the list (TimedTrafficLights // depends on this at the moment...) currentNodeGroup.Remove(masterNodeId); currentNodeGroup.Insert(0, masterNodeId); // update the saved node group and master-slave info nodeGroupByMasterNodeId[masterNodeId] = currentNodeGroup; foreach (ushort nodeId in currentNodeGroup) { masterNodeIdBySlaveNodeId[nodeId] = masterNodeId; } } catch (Exception e) { Log.WarningFormat( "Error building timed traffic light group for TimedNode {0} (NodeGroup: {1}): {2}", cnfTimedLights.nodeId, string.Join(", ", cnfTimedLights.nodeGroup.Select(x => x.ToString()).ToArray()), e); success = false; } } foreach (Configuration.TimedTrafficLights cnfTimedLights in data) { try { if (!masterNodeIdBySlaveNodeId.ContainsKey(cnfTimedLights.nodeId)) { continue; } ushort masterNodeId = masterNodeIdBySlaveNodeId[cnfTimedLights.nodeId]; List <ushort> nodeGroup = nodeGroupByMasterNodeId[masterNodeId]; #if DEBUGLOAD Log._Debug($"Adding timed light at node {cnfTimedLights.nodeId}. NodeGroup: " + $"{string.Join(", ", nodeGroup.Select(x => x.ToString()).ToArray())}"); #endif SetUpTimedTrafficLight(cnfTimedLights.nodeId, nodeGroup); int j = 0; foreach (Configuration.TimedTrafficLightsStep cnfTimedStep in cnfTimedLights.timedSteps) { #if DEBUGLOAD Log._Debug($"Loading timed step {j} at node {cnfTimedLights.nodeId}"); #endif ITimedTrafficLightsStep step = TrafficLightSimulations[cnfTimedLights.nodeId].timedLight.AddStep( cnfTimedStep.minTime, cnfTimedStep.maxTime, (StepChangeMetric)cnfTimedStep.changeMetric, cnfTimedStep.waitFlowBalance); foreach (KeyValuePair <ushort, Configuration.CustomSegmentLights> e in cnfTimedStep.segmentLights) { if (!Services.NetService.IsSegmentValid(e.Key)) { continue; } e.Value.nodeId = cnfTimedLights.nodeId; #if DEBUGLOAD Log._Debug($"Loading timed step {j}, segment {e.Key} at node {cnfTimedLights.nodeId}"); #endif ICustomSegmentLights lights = null; if (!step.CustomSegmentLights.TryGetValue(e.Key, out lights)) { #if DEBUGLOAD Log._Debug($"No segment lights found at timed step {j} for segment " + $"{e.Key}, node {cnfTimedLights.nodeId}"); #endif continue; } Configuration.CustomSegmentLights cnfLights = e.Value; #if DEBUGLOAD Log._Debug($"Loading pedestrian light @ seg. {e.Key}, step {j}: " + $"{cnfLights.pedestrianLightState} {cnfLights.manualPedestrianMode}"); #endif lights.ManualPedestrianMode = cnfLights.manualPedestrianMode; lights.PedestrianLightState = cnfLights.pedestrianLightState; bool first = true; // v1.10.2 transitional code foreach (KeyValuePair <ExtVehicleType, Configuration.CustomSegmentLight> e2 in cnfLights.customLights) { #if DEBUGLOAD Log._Debug($"Loading timed step {j}, segment {e.Key}, vehicleType " + $"{e2.Key} at node {cnfTimedLights.nodeId}"); #endif if (!lights.CustomLights.TryGetValue( LegacyExtVehicleType.ToNew(e2.Key), out ICustomSegmentLight light)) { #if DEBUGLOAD Log._Debug($"No segment light found for timed step {j}, segment " + $"{e.Key}, vehicleType {e2.Key} at node {cnfTimedLights.nodeId}"); #endif // v1.10.2 transitional code START if (first) { first = false; if (!lights.CustomLights.TryGetValue( CustomSegmentLights .DEFAULT_MAIN_VEHICLETYPE, out light)) { #if DEBUGLOAD Log._Debug($"No segment light found for timed step {j}, " + $"segment {e.Key}, DEFAULT vehicleType {CustomSegmentLights.DEFAULT_MAIN_VEHICLETYPE} " + $"at node {cnfTimedLights.nodeId}"); #endif continue; } } else { // v1.10.2 transitional code END continue; // v1.10.2 transitional code START } // v1.10.2 transitional code END } Configuration.CustomSegmentLight cnfLight = e2.Value; light.InternalCurrentMode = (LightMode)cnfLight.currentMode; // TODO improve & remove light.SetStates( cnfLight.mainLight, cnfLight.leftLight, cnfLight.rightLight, false); } } ++j; } } catch (Exception e) { // ignore, as it's probably corrupt save data. it'll be culled on next save Log.Warning($"Error loading data from TimedNode (new method): {e}"); success = false; } } foreach (Configuration.TimedTrafficLights cnfTimedLights in data) { try { ITimedTrafficLights timedNode = TrafficLightSimulations[cnfTimedLights.nodeId].timedLight; timedNode.Housekeeping(); if (cnfTimedLights.started) { timedNode.Start(cnfTimedLights.currentStep); } } catch (Exception e) { Log.Warning($"Error starting timed light @ {cnfTimedLights.nodeId}: {e}"); success = false; } } return(success); }
/// <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); }
public List <Configuration.TimedTrafficLights> SaveData(ref bool success) { List <Configuration.TimedTrafficLights> ret = new List <Configuration.TimedTrafficLights>(); for (uint nodeId = 0; nodeId < NetManager.MAX_NODE_COUNT; ++nodeId) { try { ITrafficLightSimulation sim = GetNodeSimulation((ushort)nodeId); if (sim == null || !sim.IsTimedLight()) { continue; } Log._Debug($"Going to save timed light at node {nodeId}."); var timedNode = sim.TimedLight; timedNode.OnGeometryUpdate(); Configuration.TimedTrafficLights cnfTimedLights = new Configuration.TimedTrafficLights(); ret.Add(cnfTimedLights); cnfTimedLights.nodeId = timedNode.NodeId; cnfTimedLights.nodeGroup = new List <ushort>(timedNode.NodeGroup); cnfTimedLights.started = timedNode.IsStarted(); int stepIndex = timedNode.CurrentStep; if (timedNode.IsStarted() && timedNode.GetStep(timedNode.CurrentStep).IsInEndTransition()) { // if in end transition save the next step stepIndex = (stepIndex + 1) % timedNode.NumSteps(); } cnfTimedLights.currentStep = stepIndex; cnfTimedLights.timedSteps = new List <Configuration.TimedTrafficLightsStep>(); for (var j = 0; j < timedNode.NumSteps(); j++) { Log._Debug($"Saving timed light step {j} at node {nodeId}."); ITimedTrafficLightsStep timedStep = timedNode.GetStep(j); Configuration.TimedTrafficLightsStep cnfTimedStep = new Configuration.TimedTrafficLightsStep(); cnfTimedLights.timedSteps.Add(cnfTimedStep); cnfTimedStep.minTime = timedStep.MinTime; cnfTimedStep.maxTime = timedStep.MaxTime; cnfTimedStep.changeMetric = (int)timedStep.ChangeMetric; cnfTimedStep.waitFlowBalance = timedStep.WaitFlowBalance; cnfTimedStep.segmentLights = new Dictionary <ushort, Configuration.CustomSegmentLights>(); foreach (KeyValuePair <ushort, ICustomSegmentLights> e in timedStep.CustomSegmentLights) { Log._Debug($"Saving timed light step {j}, segment {e.Key} at node {nodeId}."); ICustomSegmentLights segLights = e.Value; Configuration.CustomSegmentLights cnfSegLights = new Configuration.CustomSegmentLights(); ushort lightsNodeId = segLights.NodeId; if (lightsNodeId == 0 || lightsNodeId != timedNode.NodeId) { Log.Warning($"Inconsistency detected: Timed traffic light @ node {timedNode.NodeId} contains custom traffic lights for the invalid segment ({segLights.SegmentId}) at step {j}: nId={lightsNodeId}"); continue; } cnfSegLights.nodeId = lightsNodeId; // TODO not needed cnfSegLights.segmentId = segLights.SegmentId; // TODO not needed cnfSegLights.customLights = new Dictionary <ExtVehicleType, Configuration.CustomSegmentLight>(); cnfSegLights.pedestrianLightState = segLights.PedestrianLightState; cnfSegLights.manualPedestrianMode = segLights.ManualPedestrianMode; cnfTimedStep.segmentLights.Add(e.Key, cnfSegLights); Log._Debug($"Saving pedestrian light @ seg. {e.Key}, step {j}: {cnfSegLights.pedestrianLightState} {cnfSegLights.manualPedestrianMode}"); foreach (KeyValuePair <ExtVehicleType, ICustomSegmentLight> e2 in segLights.CustomLights) { Log._Debug($"Saving timed light step {j}, segment {e.Key}, vehicleType {e2.Key} at node {nodeId}."); ICustomSegmentLight segLight = e2.Value; Configuration.CustomSegmentLight cnfSegLight = new Configuration.CustomSegmentLight(); cnfSegLights.customLights.Add(e2.Key, cnfSegLight); cnfSegLight.nodeId = lightsNodeId; // TODO not needed cnfSegLight.segmentId = segLights.SegmentId; // TODO not needed cnfSegLight.currentMode = (int)segLight.CurrentMode; cnfSegLight.leftLight = segLight.LightLeft; cnfSegLight.mainLight = segLight.LightMain; cnfSegLight.rightLight = segLight.LightRight; } } } } catch (Exception e) { Log.Error($"Exception occurred while saving timed traffic light @ {nodeId}: {e.ToString()}"); success = false; } } return(ret); }
/// <summary> /// Configures traffic light for and for all lane types at input segmentId, nodeId, and step. /// </summary> /// <param name="step"></param> /// <param name="nodeId"></param> /// <param name="segmentId"></param> /// <param name="m">Determines which directions are green</param> private static void SetupHelper(ITimedTrafficLightsStep step, ushort nodeId, ushort segmentId, GreenDir m) { bool startNode = (bool)ExtSegmentManager.Instance.IsStartNode(segmentId, nodeId); ref NetNode netNode = ref nodeId.ToNode();