public override void OnPrimaryClickOverlay() { if (SelectedNodeId != 0) { return; } TrafficLightSimulation sim = TrafficLightSimulation.GetNodeSimulation(HoveredNodeId); if (sim == null || !sim.IsTimedLight()) { if ((Singleton <NetManager> .instance.m_nodes.m_buffer[HoveredNodeId].m_flags & NetNode.Flags.TrafficLights) == NetNode.Flags.None) { TrafficPriority.RemovePrioritySegments(HoveredNodeId); Flags.setNodeTrafficLight(HoveredNodeId, true); } SelectedNodeId = HoveredNodeId; sim = TrafficLightSimulation.AddNodeToSimulation(SelectedNodeId); sim.SetupManualTrafficLight(); /*for (var s = 0; s < 8; s++) { * var segment = Singleton<NetManager>.instance.m_nodes.m_buffer[SelectedNodeId].GetSegment(s); * if (segment != 0 && !TrafficPriority.IsPrioritySegment(SelectedNodeId, segment)) { * TrafficPriority.AddPrioritySegment(SelectedNodeId, segment, SegmentEnd.PriorityType.None); * } * }*/ } else { MainTool.ShowTooltip(Translation.GetString("NODE_IS_TIMED_LIGHT"), Singleton <NetManager> .instance.m_nodes.m_buffer[HoveredNodeId].m_position); } }
internal void Housekeeping() { if (TrafficManagerTool.GetToolMode() != ToolMode.AddPrioritySigns && TrafficLightSimulation.GetNodeSimulation(NodeId) == null && Type == PriorityType.None) { TrafficPriority.RemovePrioritySegments(NodeId); } }
public bool SetUpTimedTrafficLight(ref TrafficLightSimulation sim, IEnumerable <ushort> nodeGroup) { if (sim.IsManualLight()) { DestroyManualTrafficLight(ref sim); } if (sim.IsTimedLight()) { return(false); } Constants.ServiceFactory.NetService.ProcessNode( sim.nodeId, (ushort nId, ref NetNode node) => { Constants.ManagerFactory.TrafficLightManager.AddTrafficLight( nId, ref node); return(true); }); Constants.ManagerFactory.CustomSegmentLightsManager.AddNodeLights(sim.nodeId); sim.timedLight = new TimedTrafficLights(sim.nodeId, nodeGroup); sim.type = TrafficLightSimulationType.Timed; return(true); }
public override void OnPrimaryClickOverlay() { if (IsCursorInPanel()) { return; } if (HoveredNodeId == 0) { return; } if ((Singleton <NetManager> .instance.m_nodes.m_buffer[HoveredNodeId].m_flags & NetNode.Flags.Junction) == NetNode.Flags.None) { return; } TrafficLightSimulation sim = TrafficLightSimulationManager.Instance.GetNodeSimulation(HoveredNodeId); if (sim != null && sim.IsTimedLight()) { MainTool.ShowTooltip(Translation.GetString("NODE_IS_TIMED_LIGHT"), Singleton <NetManager> .instance.m_nodes.m_buffer[HoveredNodeId].m_position); return; } TrafficPriorityManager.Instance.RemovePrioritySegments(HoveredNodeId); TrafficLightManager.Instance.ToggleTrafficLight(HoveredNodeId); }
public void CustomNodeSimulationStep(ushort nodeId, ref NetNode data) { if (simStartFrame == 0) { simStartFrame = Singleton <SimulationManager> .instance.m_currentFrameIndex; } try { if (TrafficLightTool.getToolMode() != ToolMode.AddPrioritySigns) { try { TrafficPriority.nodeHousekeeping(nodeId); } catch (Exception e) { Log.Error($"Error occured while housekeeping node {nodeId}: " + e.ToString()); } } TrafficPriority.TrafficLightSimulationStep(); var nodeSim = TrafficLightSimulation.GetNodeSimulation(nodeId); if (nodeSim == null || !nodeSim.IsSimulationActive()) { OriginalSimulationStep(nodeId, ref data); } } catch (Exception e) { Log.Warning($"CustomNodeSimulationStep: An error occurred: {e.ToString()}"); } }
/// <summary> /// Destroys the traffic light and removes it /// </summary> /// <param name="nodeId"></param> /// <param name="destroyGroup"></param> public void RemoveNodeFromSimulation(ushort nodeId, bool destroyGroup, bool removeTrafficLight) { #if DEBUG Log.Warning($"TrafficLightSimulationManager.RemoveNodeFromSimulation({nodeId}, {destroyGroup}, {removeTrafficLight}) called."); #endif TrafficLightSimulation sim = TrafficLightSimulations[nodeId]; if (sim == null) { return; } TrafficLightManager tlm = TrafficLightManager.Instance; if (sim.TimedLight != null) { // remove/destroy all timed traffic lights in group List <ushort> oldNodeGroup = new List <ushort>(sim.TimedLight.NodeGroup); foreach (var timedNodeId in oldNodeGroup) { var otherNodeSim = GetNodeSimulation(timedNodeId); if (otherNodeSim == null) { continue; } if (destroyGroup || timedNodeId == nodeId) { //Log._Debug($"Slave: Removing simulation @ node {timedNodeId}"); otherNodeSim.DestroyTimedTrafficLight(); otherNodeSim.DestroyManualTrafficLight(); ((TrafficLightSimulation)otherNodeSim).NodeGeoUnsubscriber?.Dispose(); RemoveNodeFromSimulation(timedNodeId); if (removeTrafficLight) { Constants.ServiceFactory.NetService.ProcessNode(timedNodeId, delegate(ushort nId, ref NetNode node) { tlm.RemoveTrafficLight(timedNodeId, ref node); return(true); }); } } else { otherNodeSim.TimedLight.RemoveNodeFromGroup(nodeId); } } } //Flags.setNodeTrafficLight(nodeId, false); //sim.DestroyTimedTrafficLight(); sim.DestroyManualTrafficLight(); sim.NodeGeoUnsubscriber?.Dispose(); RemoveNodeFromSimulation(nodeId); if (removeTrafficLight) { Constants.ServiceFactory.NetService.ProcessNode(nodeId, delegate(ushort nId, ref NetNode node) { tlm.RemoveTrafficLight(nodeId, ref node); return(true); }); } }
//public Dictionary<ushort, TrafficLightSimulation> TrafficLightSimulations = new Dictionary<ushort, TrafficLightSimulation>(); private TrafficLightSimulationManager() { TrafficLightSimulations = new TrafficLightSimulation[NetManager.MAX_NODE_COUNT]; for (int i = 0; i < TrafficLightSimulations.Length; ++i) { TrafficLightSimulations[i] = new TrafficLightSimulation((ushort)i); } }
public bool CustomCheckTrafficLights(ushort node, ushort segment) { var nodeSimulation = TrafficLightSimulation.GetNodeSimulation(node); var instance = Singleton <NetManager> .instance; var currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex; var num = (uint)((node << 8) / 32768); var num2 = currentFrameIndex - num & 255u; // NON-STOCK CODE START // RoadBaseAI.TrafficLightState pedestrianLightState; CustomSegmentLights lights = CustomTrafficLights.GetSegmentLights(node, segment); if (lights == null || nodeSimulation == null || !nodeSimulation.IsSimulationActive()) { RoadBaseAI.TrafficLightState vehicleLightState; bool vehicles; bool pedestrians; RoadBaseAI.GetTrafficLightState(node, ref instance.m_segments.m_buffer[segment], currentFrameIndex - num, out vehicleLightState, out pedestrianLightState, out vehicles, out pedestrians); if ((pedestrianLightState == RoadBaseAI.TrafficLightState.GreenToRed || pedestrianLightState == RoadBaseAI.TrafficLightState.Red) && !pedestrians && num2 >= 196u) { RoadBaseAI.SetTrafficLightState(node, ref instance.m_segments.m_buffer[segment], currentFrameIndex - num, vehicleLightState, pedestrianLightState, vehicles, true); return(true); } } else { if (lights.PedestrianLightState == null) { Log._Debug($"A pedestrian wants to cross node {node} at segment {segment} but there is no pedestrian traffic light!"); pedestrianLightState = lights.GetAutoPedestrianLightState(); } else { pedestrianLightState = (RoadBaseAI.TrafficLightState)lights.PedestrianLightState; } } // NON-STOCK CODE END // switch (pedestrianLightState) { case RoadBaseAI.TrafficLightState.RedToGreen: if (num2 < 60u) { return(false); } break; case RoadBaseAI.TrafficLightState.Red: case RoadBaseAI.TrafficLightState.GreenToRed: return(false); } return(true); }
/// <summary> /// Adds a traffic light simulation to the node with the given id /// </summary> /// <param name="nodeId"></param> public TrafficLightSimulation AddNodeToSimulation(ushort nodeId) { if (HasSimulation(nodeId)) { return(TrafficLightSimulations[nodeId]); } TrafficLightSimulations[nodeId] = new TrafficLightSimulation(nodeId); SubscribeToNodeGeometry(nodeId); return(TrafficLightSimulations[nodeId]); }
public bool HasActiveSimulation(ushort nodeId) { TrafficLightSimulation sim = TrafficLightSimulations[nodeId]; if (sim == null) { return(false); } return(sim.IsManualLight() || sim.IsTimedLightActive()); }
public bool HasTimedSimulation(ushort nodeId) { TrafficLightSimulation sim = TrafficLightSimulations[nodeId]; if (sim == null) { return(false); } return(sim.IsTimedLight()); }
/// <summary> /// Adds a traffic light simulation to the node with the given id /// </summary> /// <param name="nodeId"></param> public ITrafficLightSimulation AddNodeToSimulation(ushort nodeId) { TrafficLightSimulation sim = TrafficLightSimulations[nodeId]; if (sim != null) { return(sim); } TrafficLightSimulations[nodeId] = sim = new TrafficLightSimulation(nodeId); SubscribeToNodeGeometry(nodeId); return(sim); }
public static void GetTrafficLightState(ushort vehicleId, ref Vehicle vehicleData, ushort nodeId, ushort fromSegmentId, ushort toSegmentId, ref NetSegment segmentData, uint frame, out RoadBaseAI.TrafficLightState vehicleLightState, out RoadBaseAI.TrafficLightState pedestrianLightState) { TrafficLightSimulation nodeSim = TrafficLightSimulation.GetNodeSimulation(nodeId); if (nodeSim == null || !nodeSim.IsSimulationActive()) { RoadBaseAI.GetTrafficLightState(nodeId, ref segmentData, frame, out vehicleLightState, out pedestrianLightState); } else { GetCustomTrafficLightState(vehicleId, ref vehicleData, nodeId, fromSegmentId, toSegmentId, out vehicleLightState, out pedestrianLightState, nodeSim); } }
/// <summary> /// Destroys the traffic light and removes it /// </summary> /// <param name="nodeId"></param> /// <param name="destroyGroup"></param> public void RemoveNodeFromSimulation(ushort nodeId, bool destroyGroup, bool removeTrafficLight) { if (!HasSimulation(nodeId)) { return; } TrafficLightSimulation sim = TrafficLightSimulations[nodeId]; TrafficLightManager tlm = TrafficLightManager.Instance; if (sim.TimedLight != null) { // remove/destroy all timed traffic lights in group List <ushort> oldNodeGroup = new List <ushort>(sim.TimedLight.NodeGroup); foreach (var timedNodeId in oldNodeGroup) { var otherNodeSim = GetNodeSimulation(timedNodeId); if (otherNodeSim == null) { continue; } if (destroyGroup || timedNodeId == nodeId) { //Log._Debug($"Slave: Removing simulation @ node {timedNodeId}"); otherNodeSim.DestroyTimedTrafficLight(); otherNodeSim.DestroyManualTrafficLight(); otherNodeSim.NodeGeoUnsubscriber?.Dispose(); RemoveNodeFromSimulation(timedNodeId); if (removeTrafficLight) { tlm.RemoveTrafficLight(timedNodeId); } } else { otherNodeSim.TimedLight.RemoveNodeFromGroup(nodeId); } } } //Flags.setNodeTrafficLight(nodeId, false); //sim.DestroyTimedTrafficLight(); sim.DestroyManualTrafficLight(); sim.NodeGeoUnsubscriber?.Dispose(); RemoveNodeFromSimulation(nodeId); if (removeTrafficLight) { tlm.RemoveTrafficLight(nodeId); } }
public bool SetUpManualTrafficLight(ref TrafficLightSimulation sim) { if (sim.IsTimedLight()) { return(false); } Constants.ManagerFactory.TrafficLightManager.AddTrafficLight( sim.nodeId, ref sim.nodeId.ToNode()); Constants.ManagerFactory.CustomSegmentLightsManager.AddNodeLights(sim.nodeId); sim.type = TrafficLightSimulationType.Manual; return(true); }
public bool DestroyManualTrafficLight(ref TrafficLightSimulation sim) { if (sim.IsTimedLight()) { return(false); } if (!sim.IsManualLight()) { return(false); } sim.type = TrafficLightSimulationType.None; Constants.ManagerFactory.CustomSegmentLightsManager.RemoveNodeLights(sim.nodeId); return(true); }
public bool DestroyTimedTrafficLight(ref TrafficLightSimulation sim) { if (!sim.IsTimedLight()) { return(false); } sim.type = TrafficLightSimulationType.None; ITimedTrafficLights timedLight = sim.timedLight; sim.timedLight = null; timedLight?.Destroy(); return(true); }
public override void Cleanup() { if (SelectedNodeId == 0) { return; } var nodeSimulation = TrafficLightSimulation.GetNodeSimulation(SelectedNodeId); if (nodeSimulation == null || !nodeSimulation.IsManualLight()) { return; } nodeSimulation.DestroyManualTrafficLight(); TrafficLightSimulation.RemoveNodeFromSimulation(SelectedNodeId, true); }
public TrafficLightSimulation GetNodeSimulation(ushort nodeId) { #if TRACE Singleton <CodeProfiler> .instance.Start("TrafficLightSimulation.GetNodeSimulation"); #endif TrafficLightSimulation ret = null; if (TrafficLightSimulations.ContainsKey(nodeId)) { ret = TrafficLightSimulations[nodeId]; } #if TRACE Singleton <CodeProfiler> .instance.Stop("TrafficLightSimulation.GetNodeSimulation"); #endif return(ret); }
public bool SetUpManualTrafficLight(ref TrafficLightSimulation sim) { if (sim.IsTimedLight()) { return(false); } Constants.ServiceFactory.NetService.ProcessNode( sim.nodeId, (ushort nId, ref NetNode node) => { Constants.ManagerFactory.TrafficLightManager.AddTrafficLight( nId, ref node); return(true); }); Constants.ManagerFactory.CustomSegmentLightsManager.AddNodeLights(sim.nodeId); sim.type = TrafficLightSimulationType.Manual; return(true); }
private void RenderManualNodeOverlays(RenderManager.CameraInfo cameraInfo) { var nodeSimulation = TrafficLightSimulation.GetNodeSimulation(SelectedNodeId); for (var i = 0; i < 8; i++) { var colorGray = new Color(0.25f, 0.25f, 0.25f, 0.25f); ushort segmentId = Singleton <NetManager> .instance.m_nodes.m_buffer[SelectedNodeId].GetSegment(i); if (segmentId == 0 || (nodeSimulation != null && CustomTrafficLights.IsSegmentLight(SelectedNodeId, segmentId))) { continue; } var position = CalculateNodePositionForSegment(Singleton <NetManager> .instance.m_nodes.m_buffer[SelectedNodeId], segmentId); var width = _hoveredButton[0] == segmentId ? 11.25f : 10f; MainTool.DrawOverlayCircle(cameraInfo, colorGray, position, width, segmentId != _hoveredButton[0]); } }
public void SimulationStep() { int frame = (int)(Singleton <SimulationManager> .instance.m_currentFrameIndex & (SIM_MOD - 1)); int minIndex = frame * (NetManager.MAX_NODE_COUNT / SIM_MOD); int maxIndex = (frame + 1) * (NetManager.MAX_NODE_COUNT / SIM_MOD) - 1; for (int nodeId = minIndex; nodeId <= maxIndex; ++nodeId) { try { TrafficLightSimulation nodeSim = TrafficLightSimulations[nodeId]; if (nodeSim != null && nodeSim.IsTimedLightActive()) { //Flags.applyNodeTrafficLightFlag((ushort)nodeId); nodeSim.TimedLight.SimulationStep(); } } catch (Exception ex) { Log.Warning($"Error occured while simulating traffic light @ node {nodeId}: {ex.ToString()}"); } } }
public override void Cleanup() { return; if (SelectedNodeId == 0) { return; } if (NetworkInterface.Network.selectedNodeIds.Contains(SelectedNodeId)) { return; } var nodeSimulation = TrafficLightSimulation.GetNodeSimulation(SelectedNodeId); if (nodeSimulation == null || !nodeSimulation.IsManualLight()) { return; } nodeSimulation.DestroyManualTrafficLight(); TrafficLightSimulation.RemoveNodeFromSimulation(SelectedNodeId, true, false); }
public override void OnClickOverlay() { if ((Singleton <NetManager> .instance.m_nodes.m_buffer[HoveredNodeId].m_flags & NetNode.Flags.Junction) != NetNode.Flags.None) { if ((Singleton <NetManager> .instance.m_nodes.m_buffer[HoveredNodeId].m_flags & NetNode.Flags.TrafficLights) != NetNode.Flags.None) { TrafficLightSimulation sim = TrafficLightSimulation.GetNodeSimulation(HoveredNodeId); if (sim != null && sim.IsTimedLight()) { MainTool.ShowTooltip(Translation.GetString("NODE_IS_TIMED_LIGHT"), Singleton <NetManager> .instance.m_nodes.m_buffer[HoveredNodeId].m_position); } else { TrafficLightSimulation.RemoveNodeFromSimulation(HoveredNodeId, true); // TODO refactor! Flags.setNodeTrafficLight(HoveredNodeId, false); // TODO refactor! } } else { TrafficPriority.RemovePrioritySegments(HoveredNodeId); Flags.setNodeTrafficLight(HoveredNodeId, true); } } }
public void ShowGUI(bool viewOnly) { try { TrafficLightSimulationManager tlsMan = TrafficLightSimulationManager.Instance; TrafficPriorityManager prioMan = TrafficPriorityManager.Instance; TrafficLightManager tlm = TrafficLightManager.Instance; bool clicked = !viewOnly?MainTool.CheckClicked() : false; var hoveredSegment = false; //Log.Message("_guiPrioritySigns called. num of prio segments: " + TrafficPriority.PrioritySegments.Count); HashSet <ushort> nodeIdsWithSigns = new HashSet <ushort>(); foreach (ushort segmentId in currentPrioritySegmentIds) { var trafficSegment = prioMan.TrafficSegments[segmentId]; if (trafficSegment == null) { continue; } SegmentGeometry geometry = SegmentGeometry.Get(segmentId); prioritySegments[0] = null; prioritySegments[1] = null; if (tlsMan.GetNodeSimulation(trafficSegment.Node1) == null) { SegmentEnd tmpSeg1 = prioMan.GetPrioritySegment(trafficSegment.Node1, segmentId); bool startNode = geometry.StartNodeId() == trafficSegment.Node1; if (tmpSeg1 != null && !geometry.IsOutgoingOneWay(startNode)) { prioritySegments[0] = tmpSeg1; nodeIdsWithSigns.Add(trafficSegment.Node1); prioMan.AddPriorityNode(trafficSegment.Node1); } } if (tlsMan.GetNodeSimulation(trafficSegment.Node2) == null) { SegmentEnd tmpSeg2 = prioMan.GetPrioritySegment(trafficSegment.Node2, segmentId); bool startNode = geometry.StartNodeId() == trafficSegment.Node2; if (tmpSeg2 != null && !geometry.IsOutgoingOneWay(startNode)) { prioritySegments[1] = tmpSeg2; nodeIdsWithSigns.Add(trafficSegment.Node2); prioMan.AddPriorityNode(trafficSegment.Node2); } } //Log.Message("init ok"); foreach (var prioritySegment in prioritySegments) { if (prioritySegment == null) { continue; } var nodeId = prioritySegment.NodeId; //Log.Message("_guiPrioritySigns: nodeId=" + nodeId); var nodePositionVector3 = Singleton <NetManager> .instance.m_nodes.m_buffer[nodeId].m_position; var camPos = Singleton <SimulationManager> .instance.m_simulationView.m_position; var diff = nodePositionVector3 - camPos; if (diff.magnitude > TrafficManagerTool.PriorityCloseLod) { continue; // do not draw if too distant } if (Singleton <NetManager> .instance.m_segments.m_buffer[segmentId].m_startNode == (ushort)nodeId) { nodePositionVector3.x += Singleton <NetManager> .instance.m_segments.m_buffer[segmentId].m_startDirection.x * 10f; nodePositionVector3.y += Singleton <NetManager> .instance.m_segments.m_buffer[segmentId].m_startDirection.y * 10f; nodePositionVector3.z += Singleton <NetManager> .instance.m_segments.m_buffer[segmentId].m_startDirection.z * 10f; } else { nodePositionVector3.x += Singleton <NetManager> .instance.m_segments.m_buffer[segmentId].m_endDirection.x * 10f; nodePositionVector3.y += Singleton <NetManager> .instance.m_segments.m_buffer[segmentId].m_endDirection.y * 10f; nodePositionVector3.z += Singleton <NetManager> .instance.m_segments.m_buffer[segmentId].m_endDirection.z * 10f; } var nodeScreenPosition = Camera.main.WorldToScreenPoint(nodePositionVector3); nodeScreenPosition.y = Screen.height - nodeScreenPosition.y; if (nodeScreenPosition.z < 0) { continue; } var zoom = 1.0f / diff.magnitude * 100f * MainTool.GetBaseZoom(); var size = 110f * zoom; var guiColor = GUI.color; var nodeBoundingBox = new Rect(nodeScreenPosition.x - size / 2, nodeScreenPosition.y - size / 2, size, size); hoveredSegment = !viewOnly && TrafficManagerTool.IsMouseOver(nodeBoundingBox); if (hoveredSegment) { // mouse hovering over sign guiColor.a = 0.8f; } else { guiColor.a = 0.5f; size = 90f * zoom; } var nodeDrawingBox = new Rect(nodeScreenPosition.x - size / 2, nodeScreenPosition.y - size / 2, size, size); GUI.color = guiColor; bool setUndefinedSignsToMainRoad = false; switch (prioritySegment.Type) { case SegmentEnd.PriorityType.Main: GUI.DrawTexture(nodeDrawingBox, TrafficLightToolTextureResources.SignPriorityTexture2D); if (clicked && hoveredSegment) { //Log._Debug("Click on node " + nodeId + ", segment " + segmentId + " to change prio type (1)"); //Log.Message("PrioritySegment.Type = Yield"); prioritySegment.Type = SegmentEnd.PriorityType.Yield; setUndefinedSignsToMainRoad = true; clicked = false; } break; case SegmentEnd.PriorityType.Yield: GUI.DrawTexture(nodeDrawingBox, TrafficLightToolTextureResources.SignYieldTexture2D); if (clicked && hoveredSegment) { //Log._Debug("Click on node " + nodeId + ", segment " + segmentId + " to change prio type (2)"); prioritySegment.Type = SegmentEnd.PriorityType.Stop; setUndefinedSignsToMainRoad = true; clicked = false; } break; case SegmentEnd.PriorityType.Stop: GUI.DrawTexture(nodeDrawingBox, TrafficLightToolTextureResources.SignStopTexture2D); if (clicked && hoveredSegment) { //Log._Debug("Click on node " + nodeId + ", segment " + segmentId + " to change prio type (3)"); prioritySegment.Type = SegmentEnd.PriorityType.Main; clicked = false; } break; case SegmentEnd.PriorityType.None: if (viewOnly) { break; } GUI.DrawTexture(nodeDrawingBox, TrafficLightToolTextureResources.SignNoneTexture2D); if (clicked && hoveredSegment) { //Log._Debug("Click on node " + nodeId + ", segment " + segmentId + " to change prio type (4)"); //Log.Message("PrioritySegment.Type = None"); prioritySegment.Type = GetNumberOfMainRoads(nodeId, ref Singleton <NetManager> .instance.m_nodes.m_buffer[nodeId]) >= 2 ? SegmentEnd.PriorityType.Yield : SegmentEnd.PriorityType.Main; if (prioritySegment.Type == SegmentEnd.PriorityType.Yield) { setUndefinedSignsToMainRoad = true; } clicked = false; } break; } if (setUndefinedSignsToMainRoad) { foreach (var otherPrioritySegment in prioMan.GetPrioritySegments(nodeId)) { if (otherPrioritySegment.SegmentId == prioritySegment.SegmentId) { continue; } if (otherPrioritySegment.Type == SegmentEnd.PriorityType.None) { otherPrioritySegment.Type = SegmentEnd.PriorityType.Main; } } } } } if (viewOnly) { return; } ushort hoveredExistingNodeId = 0; foreach (ushort nodeId in nodeIdsWithSigns) { var nodePositionVector3 = Singleton <NetManager> .instance.m_nodes.m_buffer[nodeId].m_position; var camPos = Singleton <SimulationManager> .instance.m_simulationView.m_position; var diff = nodePositionVector3 - camPos; if (diff.magnitude > TrafficManagerTool.PriorityCloseLod) { continue; } // draw deletion button var nodeScreenPosition = Camera.main.WorldToScreenPoint(nodePositionVector3); nodeScreenPosition.y = Screen.height - nodeScreenPosition.y; if (nodeScreenPosition.z < 0) { continue; } var zoom = 1.0f / diff.magnitude * 100f * MainTool.GetBaseZoom(); var size = 90f * zoom; var nodeBoundingBox = new Rect(nodeScreenPosition.x - size / 2, nodeScreenPosition.y - size / 2, size, size); var guiColor = GUI.color; var nodeCenterHovered = TrafficManagerTool.IsMouseOver(nodeBoundingBox); if (nodeCenterHovered) { hoveredExistingNodeId = nodeId; guiColor.a = 0.8f; } else { guiColor.a = 0.5f; } GUI.color = guiColor; GUI.DrawTexture(nodeBoundingBox, TrafficLightToolTextureResources.SignRemoveTexture2D); } // add a new or delete a priority segment node if (HoveredNodeId != 0 || hoveredExistingNodeId != 0) { bool delete = false; if (hoveredExistingNodeId != 0) { delete = true; } // determine if we may add new priority signs to this node bool ok = false; TrafficLightSimulation nodeSim = tlsMan.GetNodeSimulation(HoveredNodeId); if ((Singleton <NetManager> .instance.m_nodes.m_buffer[HoveredNodeId].m_flags & NetNode.Flags.TrafficLights) == NetNode.Flags.None) { // no traffic light set ok = true; } else if (nodeSim == null || !nodeSim.IsTimedLight()) { ok = true; } if (!Flags.mayHaveTrafficLight(HoveredNodeId)) { ok = false; } if (clicked) { Log._Debug("_guiPrioritySigns: hovered+clicked @ nodeId=" + HoveredNodeId + "/" + hoveredExistingNodeId + " ok=" + ok); if (delete) { prioMan.RemovePrioritySegments(hoveredExistingNodeId); RefreshCurrentPrioritySegmentIds(); } else if (ok) { //if (!prioMan.IsPriorityNode(HoveredNodeId)) { Log._Debug("_guiPrioritySigns: adding prio segments @ nodeId=" + HoveredNodeId); tlsMan.RemoveNodeFromSimulation(HoveredNodeId, false, true); tlm.RemoveTrafficLight(HoveredNodeId); prioMan.AddPriorityNode(HoveredNodeId); RefreshCurrentPrioritySegmentIds(); //} } else if (nodeSim != null && nodeSim.IsTimedLight()) { MainTool.ShowTooltip(Translation.GetString("NODE_IS_TIMED_LIGHT"), Singleton <NetManager> .instance.m_nodes.m_buffer[HoveredNodeId].m_position); } } } } catch (Exception e) { Log.Error(e.ToString()); } }
private static void SaveTimedTrafficLight(ushort i, Configuration configuration) { try { TrafficLightSimulation sim = TrafficLightSimulation.GetNodeSimulation(i); if (sim == null || !sim.IsTimedLight()) { return; } Log._Debug($"Going to save timed light at node {i}."); var timedNode = sim.TimedLight; timedNode.handleNewSegments(); Configuration.TimedTrafficLights cnfTimedLights = new Configuration.TimedTrafficLights(); configuration.TimedLights.Add(cnfTimedLights); cnfTimedLights.nodeId = timedNode.NodeId; cnfTimedLights.nodeGroup = timedNode.NodeGroup; cnfTimedLights.started = timedNode.IsStarted(); cnfTimedLights.timedSteps = new List <Configuration.TimedTrafficLightsStep>(); for (var j = 0; j < timedNode.NumSteps(); j++) { Log._Debug($"Saving timed light step {j} at node {i}."); TimedTrafficLightsStep timedStep = timedNode.Steps[j]; Configuration.TimedTrafficLightsStep cnfTimedStep = new Configuration.TimedTrafficLightsStep(); cnfTimedLights.timedSteps.Add(cnfTimedStep); cnfTimedStep.minTime = timedStep.minTime; cnfTimedStep.maxTime = timedStep.maxTime; cnfTimedStep.waitFlowBalance = timedStep.waitFlowBalance; cnfTimedStep.segmentLights = new Dictionary <ushort, Configuration.CustomSegmentLights>(); foreach (KeyValuePair <ushort, CustomSegmentLights> e in timedStep.segmentLights) { Log._Debug($"Saving timed light step {j}, segment {e.Key} at node {i}."); CustomSegmentLights segLights = e.Value; Configuration.CustomSegmentLights cnfSegLights = new Configuration.CustomSegmentLights(); cnfTimedStep.segmentLights.Add(e.Key, cnfSegLights); cnfSegLights.nodeId = segLights.NodeId; cnfSegLights.segmentId = segLights.SegmentId; cnfSegLights.customLights = new Dictionary <ExtVehicleType, Configuration.CustomSegmentLight>(); cnfSegLights.pedestrianLightState = segLights.PedestrianLightState; cnfSegLights.manualPedestrianMode = segLights.ManualPedestrianMode; Log._Debug($"Saving pedestrian light @ seg. {e.Key}, step {j}: {cnfSegLights.pedestrianLightState} {cnfSegLights.manualPedestrianMode}"); foreach (KeyValuePair <Traffic.ExtVehicleType, CustomSegmentLight> e2 in segLights.CustomLights) { Log._Debug($"Saving timed light step {j}, segment {e.Key}, vehicleType {e2.Key} at node {i}."); CustomSegmentLight segLight = e2.Value; Configuration.CustomSegmentLight cnfSegLight = new Configuration.CustomSegmentLight(); cnfSegLights.customLights.Add(e2.Key, cnfSegLight); cnfSegLight.nodeId = segLight.NodeId; cnfSegLight.segmentId = segLight.SegmentId; cnfSegLight.currentMode = (int)segLight.CurrentMode; cnfSegLight.leftLight = segLight.LightLeft; cnfSegLight.mainLight = segLight.LightMain; cnfSegLight.rightLight = segLight.LightRight; } } } } catch (Exception e) { Log.Error($"Error adding TimedTrafficLights to save {e.Message}"); } }
public override void OnSaveData() { Log.Info("Recalculating segment geometries"); SegmentGeometry.OnBeforeSaveData(); Log.Info("Saving Mod Data."); var configuration = new Configuration(); if (TrafficPriority.TrafficSegments != null) { for (ushort i = 0; i < Singleton <NetManager> .instance.m_segments.m_size; i++) { try { SavePrioritySegment(i, configuration); } catch (Exception e) { Log.Error($"Exception occurred while saving priority segment @ {i}: {e.ToString()}"); } try { SaveSegmentNodeFlags(i, configuration); } catch (Exception e) { Log.Error($"Exception occurred while saving segment node flags @ {i}: {e.ToString()}"); } } } for (ushort i = 0; i < Singleton <NetManager> .instance.m_nodes.m_size; i++) { /*if (TrafficLightSimulation.LightSimulationByNodeId != null) { * SaveTrafficLightSimulation(i, configuration); * }*/ /*if (TrafficLightsManual.ManualSegments != null) { * SaveManualTrafficLight(i, configuration); * }*/ TrafficLightSimulation sim = TrafficLightSimulation.GetNodeSimulation(i); if (sim != null && sim.IsTimedLight()) { try { SaveTimedTrafficLight(i, configuration); } catch (Exception e) { Log.Error($"Exception occurred while saving timed traffic light @ {i}: {e.ToString()}"); } // TODO save new traffic lights } try { SaveNodeLights(i, configuration); } catch (Exception e) { Log.Error($"Exception occurred while saving node traffic light @ {i}: {e.ToString()}"); } } #if !TAM if (LoadingExtension.IsPathManagerCompatible) { #endif for (uint i = 0; i < Singleton <NetManager> .instance.m_lanes.m_buffer.Length; i++) { try { SaveLaneData(i, configuration); } catch (Exception e) { Log.Error($"Exception occurred while saving lane data @ {i}: {e.ToString()}"); } } #if !TAM } #endif foreach (KeyValuePair <uint, ushort> e in Flags.getAllLaneSpeedLimits()) { try { SaveLaneSpeedLimit(new Configuration.LaneSpeedLimit(e.Key, e.Value), configuration); } catch (Exception ex) { Log.Error($"Exception occurred while saving lane speed limit @ {e.Key}: {ex.ToString()}"); } } foreach (KeyValuePair <uint, ExtVehicleType> e in Flags.getAllLaneAllowedVehicleTypes()) { try { SaveLaneAllowedVehicleTypes(new Configuration.LaneVehicleTypes(e.Key, e.Value), configuration); } catch (Exception ex) { Log.Error($"Exception occurred while saving lane vehicle restrictions @ {e.Key}: {ex.ToString()}"); } } var binaryFormatter = new BinaryFormatter(); var memoryStream = new MemoryStream(); try { binaryFormatter.Serialize(memoryStream, configuration); memoryStream.Position = 0; Log.Info($"Save data byte length {memoryStream.Length}"); _serializableData.SaveData(DataId, memoryStream.ToArray()); // save options _serializableData.SaveData("TMPE_Options", new byte[] { (byte)Options.simAccuracy, (byte)0, //Options.laneChangingRandomization, (byte)Options.recklessDrivers, (byte)(Options.relaxedBusses ? 1 : 0), (byte)(Options.nodesOverlay ? 1 : 0), (byte)(Options.allowEnterBlockedJunctions ? 1 : 0), (byte)(Options.advancedAI ? 1 : 0), (byte)(Options.highwayRules ? 1 : 0), (byte)(Options.prioritySignsOverlay ? 1 : 0), (byte)(Options.timedLightsOverlay ? 1 : 0), (byte)(Options.speedLimitsOverlay ? 1 : 0), (byte)(Options.vehicleRestrictionsOverlay ? 1 : 0), (byte)(Options.strongerRoadConditionEffects ? 1 : 0), (byte)(Options.allowUTurns ? 1 : 0), (byte)(Options.allowLaneChangesWhileGoingStraight ? 1 : 0), (byte)(Options.enableDespawning ? 1 : 0), (byte)(Options.IsDynamicPathRecalculationActive() ? 1 : 0), (byte)(Options.connectedLanesOverlay ? 1 : 0) }); } catch (Exception ex) { Log.Error("Unexpected error saving data: " + ex.Message); } finally { memoryStream.Close(); } }
private static void LoadDataState() { Log.Info("Loading State from Config"); if (_configuration == null) { Log.Warning("Configuration NULL, Couldn't load save data. Possibly a new game?"); return; } // load priority segments if (_configuration.PrioritySegments != null) { Log.Info($"Loading {_configuration.PrioritySegments.Count()} priority segments"); foreach (var segment in _configuration.PrioritySegments) { try { if (segment.Length < 3) { continue; } #if DEBUG bool debug = segment[0] == 13630; #endif if ((SegmentEnd.PriorityType)segment[2] == SegmentEnd.PriorityType.None) { #if DEBUG if (debug) { Log._Debug($"Loading priority segment: Not adding 'None' priority segment: {segment[1]} @ node {segment[0]}"); } #endif continue; } if ((Singleton <NetManager> .instance.m_nodes.m_buffer[segment[0]].m_flags & NetNode.Flags.Created) == NetNode.Flags.None) { #if DEBUG if (debug) { Log._Debug($"Loading priority segment: node {segment[0]} is invalid"); } #endif continue; } if ((Singleton <NetManager> .instance.m_segments.m_buffer[segment[1]].m_flags & NetSegment.Flags.Created) == NetSegment.Flags.None) { #if DEBUG if (debug) { Log._Debug($"Loading priority segment: segment {segment[1]} @ node {segment[0]} is invalid"); } #endif continue; } if (TrafficPriority.IsPrioritySegment((ushort)segment[0], (ushort)segment[1])) { #if DEBUG if (debug) { Log._Debug($"Loading priority segment: segment {segment[1]} @ node {segment[0]} is already a priority segment"); } #endif TrafficPriority.GetPrioritySegment((ushort)segment[0], (ushort)segment[1]).Type = (SegmentEnd.PriorityType)segment[2]; continue; } #if DEBUG Log._Debug($"Adding Priority Segment of type: {segment[2].ToString()} to segment {segment[1]} @ node {segment[0]}"); #endif TrafficPriority.AddPrioritySegment((ushort)segment[0], (ushort)segment[1], (SegmentEnd.PriorityType)segment[2]); } catch (Exception e) { // ignore, as it's probably corrupt save data. it'll be culled on next save Log.Warning("Error loading data from Priority segments: " + e.ToString()); } } } else { Log.Warning("Priority segments data structure undefined!"); } // load vehicle restrictions (warning: has to be done before loading timed lights!) if (_configuration.LaneAllowedVehicleTypes != null) { Log.Info($"Loading lane vehicle restriction data. {_configuration.LaneAllowedVehicleTypes.Count} elements"); foreach (Configuration.LaneVehicleTypes laneVehicleTypes in _configuration.LaneAllowedVehicleTypes) { try { ExtVehicleType baseMask = VehicleRestrictionsManager.GetBaseMask(laneVehicleTypes.laneId); ExtVehicleType maskedType = laneVehicleTypes.vehicleTypes & baseMask; Log._Debug($"Loading lane vehicle restriction: lane {laneVehicleTypes.laneId} = {laneVehicleTypes.vehicleTypes}, masked = {maskedType}"); if (maskedType != baseMask) { Flags.setLaneAllowedVehicleTypes(laneVehicleTypes.laneId, maskedType); } else { Log._Debug($"Masked type does not differ from base type. Ignoring."); } } catch (Exception e) { // ignore, as it's probably corrupt save data. it'll be culled on next save Log.Warning("Error loading data from vehicle restrictions: " + e.ToString()); } } } else { Log.Warning("Vehicle restrctions structure undefined!"); } var timedStepCount = 0; var timedStepSegmentCount = 0; NetManager netManager = Singleton <NetManager> .instance; if (_configuration.TimedLights != null) { Log.Info($"Loading {_configuration.TimedLights.Count()} timed traffic lights (new method)"); foreach (Configuration.TimedTrafficLights cnfTimedLights in _configuration.TimedLights) { try { if ((Singleton <NetManager> .instance.m_nodes.m_buffer[cnfTimedLights.nodeId].m_flags & NetNode.Flags.Created) == NetNode.Flags.None) { continue; } Flags.setNodeTrafficLight(cnfTimedLights.nodeId, true); Log._Debug($"Adding Timed Node at node {cnfTimedLights.nodeId}"); TrafficLightSimulation sim = TrafficLightSimulation.AddNodeToSimulation(cnfTimedLights.nodeId); sim.SetupTimedTrafficLight(cnfTimedLights.nodeGroup); var timedNode = sim.TimedLight; int j = 0; foreach (Configuration.TimedTrafficLightsStep cnfTimedStep in cnfTimedLights.timedSteps) { Log._Debug($"Loading timed step {j} at node {cnfTimedLights.nodeId}"); TimedTrafficLightsStep step = timedNode.AddStep(cnfTimedStep.minTime, cnfTimedStep.maxTime, cnfTimedStep.waitFlowBalance); foreach (KeyValuePair <ushort, Configuration.CustomSegmentLights> e in cnfTimedStep.segmentLights) { Log._Debug($"Loading timed step {j}, segment {e.Key} at node {cnfTimedLights.nodeId}"); CustomSegmentLights lights = null; if (!step.segmentLights.TryGetValue(e.Key, out lights)) { Log._Debug($"No segment lights found at timed step {j} for segment {e.Key}, node {cnfTimedLights.nodeId}"); continue; } Configuration.CustomSegmentLights cnfLights = e.Value; Log._Debug($"Loading pedestrian light @ seg. {e.Key}, step {j}: {cnfLights.pedestrianLightState} {cnfLights.manualPedestrianMode}"); lights.ManualPedestrianMode = cnfLights.manualPedestrianMode; lights.PedestrianLightState = cnfLights.pedestrianLightState; foreach (KeyValuePair <ExtVehicleType, Configuration.CustomSegmentLight> e2 in cnfLights.customLights) { Log._Debug($"Loading timed step {j}, segment {e.Key}, vehicleType {e2.Key} at node {cnfTimedLights.nodeId}"); CustomSegmentLight light = null; if (!lights.CustomLights.TryGetValue(e2.Key, out light)) { Log._Debug($"No segment light found for timed step {j}, segment {e.Key}, vehicleType {e2.Key} at node {cnfTimedLights.nodeId}"); continue; } Configuration.CustomSegmentLight cnfLight = e2.Value; light.CurrentMode = (CustomSegmentLight.Mode)cnfLight.currentMode; light.LightLeft = cnfLight.leftLight; light.LightMain = cnfLight.mainLight; light.LightRight = cnfLight.rightLight; } } ++j; } if (cnfTimedLights.started) { timedNode.Start(); } } 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.ToString()); } } } else if (_configuration.TimedNodes != null && _configuration.TimedNodeGroups != null) { Log.Info($"Loading {_configuration.TimedNodes.Count()} timed traffic lights (old method)"); for (var i = 0; i < _configuration.TimedNodes.Count; i++) { try { var nodeid = (ushort)_configuration.TimedNodes[i][0]; if ((Singleton <NetManager> .instance.m_nodes.m_buffer[nodeid].m_flags & NetNode.Flags.Created) == NetNode.Flags.None) { continue; } Flags.setNodeTrafficLight(nodeid, true); Log._Debug($"Adding Timed Node {i} at node {nodeid}"); var nodeGroup = new List <ushort>(); for (var j = 0; j < _configuration.TimedNodeGroups[i].Length; j++) { nodeGroup.Add(_configuration.TimedNodeGroups[i][j]); } TrafficLightSimulation sim = TrafficLightSimulation.AddNodeToSimulation(nodeid); sim.SetupTimedTrafficLight(nodeGroup); var timedNode = sim.TimedLight; timedNode.CurrentStep = _configuration.TimedNodes[i][1]; for (var j = 0; j < _configuration.TimedNodes[i][2]; j++) { var cfgstep = _configuration.TimedNodeSteps[timedStepCount]; // old (pre 1.3.0): // cfgstep[0]: time of step // cfgstep[1]: number of segments // new (post 1.3.0): // cfgstep[0]: min. time of step // cfgstep[1]: max. time of step // cfgstep[2]: number of segments int minTime = 1; int maxTime = 1; //int numSegments = 0; float waitFlowBalance = 1f; if (cfgstep.Length == 2) { minTime = cfgstep[0]; maxTime = cfgstep[0]; //numSegments = cfgstep[1]; } else if (cfgstep.Length >= 3) { minTime = cfgstep[0]; maxTime = cfgstep[1]; //numSegments = cfgstep[2]; if (cfgstep.Length == 4) { waitFlowBalance = Convert.ToSingle(cfgstep[3]) / 10f; } if (cfgstep.Length == 5) { waitFlowBalance = Convert.ToSingle(cfgstep[4]) / 1000f; } } Log._Debug($"Adding timed step to node {nodeid}: min/max: {minTime}/{maxTime}, waitFlowBalance: {waitFlowBalance}"); timedNode.AddStep(minTime, maxTime, waitFlowBalance); var step = timedNode.Steps[j]; for (var s = 0; s < 8; s++) { var segmentId = netManager.m_nodes.m_buffer[nodeid].GetSegment(s); if (segmentId <= 0) { continue; } bool tooFewSegments = (timedStepSegmentCount >= _configuration.TimedNodeStepSegments.Count); var leftLightState = tooFewSegments ? RoadBaseAI.TrafficLightState.Red : (RoadBaseAI.TrafficLightState)_configuration.TimedNodeStepSegments[timedStepSegmentCount][0]; var mainLightState = tooFewSegments ? RoadBaseAI.TrafficLightState.Red : (RoadBaseAI.TrafficLightState)_configuration.TimedNodeStepSegments[timedStepSegmentCount][1]; var rightLightState = tooFewSegments ? RoadBaseAI.TrafficLightState.Red : (RoadBaseAI.TrafficLightState)_configuration.TimedNodeStepSegments[timedStepSegmentCount][2]; var pedLightState = tooFewSegments ? RoadBaseAI.TrafficLightState.Red : (RoadBaseAI.TrafficLightState)_configuration.TimedNodeStepSegments[timedStepSegmentCount][3]; CustomSegmentLight.Mode?mode = null; if (_configuration.TimedNodeStepSegments[timedStepSegmentCount].Length >= 5) { mode = (CustomSegmentLight.Mode)_configuration.TimedNodeStepSegments[timedStepSegmentCount][4]; } foreach (KeyValuePair <ExtVehicleType, CustomSegmentLight> e in step.segmentLights[segmentId].CustomLights) { //ManualSegmentLight segmentLight = new ManualSegmentLight(step.NodeId, step.segmentIds[k], mainLightState, leftLightState, rightLightState, pedLightState); e.Value.LightLeft = leftLightState; e.Value.LightMain = mainLightState; e.Value.LightRight = rightLightState; if (mode != null) { e.Value.CurrentMode = (CustomSegmentLight.Mode)mode; } } if (step.segmentLights[segmentId].PedestrianLightState != null) { step.segmentLights[segmentId].PedestrianLightState = pedLightState; } timedStepSegmentCount++; } timedStepCount++; } if (Convert.ToBoolean(_configuration.TimedNodes[i][3])) { timedNode.Start(); } } catch (Exception e) { // ignore, as it's probably corrupt save data. it'll be culled on next save Log.Warning("Error loading data from the TimedNodes: " + e.ToString()); } } } else { Log.Warning("Timed traffic lights data structure undefined!"); } var trafficLightDefs = _configuration.NodeTrafficLights.Split(','); Log.Info($"Loading junction traffic light data"); if (trafficLightDefs.Length <= 1) { // old method Log.Info($"Using old method to load traffic light data"); var saveDataIndex = 0; for (var i = 0; i < Singleton <NetManager> .instance.m_nodes.m_buffer.Length; i++) { //Log.Message($"Adding NodeTrafficLights iteration: {i1}"); try { if ((Singleton <NetManager> .instance.m_nodes.m_buffer[i].Info.m_class.m_service != ItemClass.Service.Road && Singleton <NetManager> .instance.m_nodes.m_buffer[i].Info.m_class.m_service != ItemClass.Service.PublicTransport) || (Singleton <NetManager> .instance.m_nodes.m_buffer[i].m_flags & NetNode.Flags.Created) == NetNode.Flags.None) { continue; } // prevent overflow if (_configuration.NodeTrafficLights.Length > saveDataIndex) { var trafficLight = _configuration.NodeTrafficLights[saveDataIndex]; #if DEBUG Log._Debug("Setting traffic light flag for node " + i + ": " + (trafficLight == '1')); #endif Flags.setNodeTrafficLight((ushort)i, trafficLight == '1'); } ++saveDataIndex; } catch (Exception e) { // ignore as it's probably bad save data. Log.Warning("Error setting the NodeTrafficLights (old): " + e.Message); } } } else { // new method foreach (var split in trafficLightDefs.Select(def => def.Split(':')).Where(split => split.Length > 1)) { try { Log.Info($"Traffic light split data: {split[0]} , {split[1]}"); var nodeId = Convert.ToUInt16(split[0]); uint flag = Convert.ToUInt16(split[1]); Flags.setNodeTrafficLight(nodeId, flag > 0); } catch (Exception e) { // ignore as it's probably bad save data. Log.Warning("Error setting the NodeTrafficLights (new): " + e.Message); } } } if (_configuration.LaneFlags != null) { Log.Info($"Loading lane arrow data"); #if DEBUG Log._Debug($"LaneFlags: {_configuration.LaneFlags}"); #endif var lanes = _configuration.LaneFlags.Split(','); if (lanes.Length > 1) { foreach (var split in lanes.Select(lane => lane.Split(':')).Where(split => split.Length > 1)) { try { Log.Info($"Split Data: {split[0]} , {split[1]}"); var laneId = Convert.ToUInt32(split[0]); uint flags = Convert.ToUInt32(split[1]); //make sure we don't cause any overflows because of bad save data. if (Singleton <NetManager> .instance.m_lanes.m_buffer.Length <= laneId) { continue; } if (flags > ushort.MaxValue) { continue; } if ((Singleton <NetManager> .instance.m_lanes.m_buffer[laneId].m_flags & (ushort)NetLane.Flags.Created) == 0 || Singleton <NetManager> .instance.m_lanes.m_buffer[laneId].m_segment == 0) { continue; } //Singleton<NetManager>.instance.m_lanes.m_buffer[laneId].m_flags = fixLaneFlags(Singleton<NetManager>.instance.m_lanes.m_buffer[laneId].m_flags); uint laneArrowFlags = flags & Flags.lfr; uint origFlags = (Singleton <NetManager> .instance.m_lanes.m_buffer[laneId].m_flags & Flags.lfr); #if DEBUG Log._Debug("Setting flags for lane " + laneId + " to " + flags + " (" + ((Flags.LaneArrows)(laneArrowFlags)).ToString() + ")"); if ((origFlags | laneArrowFlags) == origFlags) // only load if setting differs from default { Log._Debug("Flags for lane " + laneId + " are original (" + ((NetLane.Flags)(origFlags)).ToString() + ")"); } #endif Flags.setLaneArrowFlags(laneId, (Flags.LaneArrows)(laneArrowFlags)); } catch (Exception e) { Log.Error($"Error loading Lane Split data. Length: {split.Length} value: {split}\nError: {e.Message}"); } } } } else { Log.Warning("Lane arrow data structure undefined!"); } // load lane connections if (_configuration.LaneConnections != null) { Log.Info($"Loading {_configuration.LaneConnections.Count()} lane connections"); foreach (Configuration.LaneConnection conn in _configuration.LaneConnections) { try { Log._Debug($"Loading lane connection: lane {conn.lowerLaneId} -> {conn.higherLaneId}"); Singleton <LaneConnectionManager> .instance.AddLaneConnection(conn.lowerLaneId, conn.higherLaneId, conn.lowerStartNode); } catch (Exception e) { // ignore, as it's probably corrupt save data. it'll be culled on next save Log.Warning("Error loading data from lane connection: " + e.ToString()); } } } else { Log.Warning("Lane connection data structure undefined!"); } // load speed limits if (_configuration.LaneSpeedLimits != null) { Log.Info($"Loading lane speed limit data. {_configuration.LaneSpeedLimits.Count} elements"); foreach (Configuration.LaneSpeedLimit laneSpeedLimit in _configuration.LaneSpeedLimits) { try { Log._Debug($"Loading lane speed limit: lane {laneSpeedLimit.laneId} = {laneSpeedLimit.speedLimit}"); Flags.setLaneSpeedLimit(laneSpeedLimit.laneId, laneSpeedLimit.speedLimit); } catch (Exception e) { // ignore, as it's probably corrupt save data. it'll be culled on next save Log.Warning("Error loading speed limits: " + e.ToString()); } } } else { Log.Warning("Lane speed limit structure undefined!"); } // Load segment-at-node flags if (_configuration.SegmentNodeConfs != null) { Log.Info($"Loading segment-at-node data. {_configuration.SegmentNodeConfs.Count} elements"); foreach (Configuration.SegmentNodeConf segNodeConf in _configuration.SegmentNodeConfs) { try { if ((Singleton <NetManager> .instance.m_segments.m_buffer[segNodeConf.segmentId].m_flags & NetSegment.Flags.Created) == NetSegment.Flags.None) { continue; } Flags.setSegmentNodeFlags(segNodeConf.segmentId, true, segNodeConf.startNodeFlags); Flags.setSegmentNodeFlags(segNodeConf.segmentId, false, segNodeConf.endNodeFlags); } catch (Exception e) { // ignore, as it's probably corrupt save data. it'll be culled on next save Log.Warning("Error loading segment-at-node config: " + e.ToString()); } } } else { Log.Warning("Segment-at-node structure undefined!"); } }
/// <summary> /// Handles vehicle path information in order to manage special nodes (nodes with priority signs or traffic lights). /// Data like "vehicle X is on segment S0 and is going to segment S1" is collected. /// </summary> /// <param name="vehicleId"></param> /// <param name="vehicleData"></param> internal static void HandleVehicle(ushort vehicleId, ref Vehicle vehicleData, bool addTraffic, bool realTraffic, byte maxUpcomingPathPositions, bool debug = false) { if (maxUpcomingPathPositions <= 0) { maxUpcomingPathPositions = 1; // we need at least one upcoming path position } var netManager = Singleton <NetManager> .instance; var lastFrameData = vehicleData.GetLastFrameData(); var lastFrameVehiclePos = lastFrameData.m_position; #if DEBUGV var camPos = Camera.main.transform.position; //debug = (lastFrameVehiclePos - camPos).sqrMagnitude < CloseLod; debug = false; List <String> logBuffer = new List <String>(); bool logme = false; #endif if ((vehicleData.m_flags & Vehicle.Flags.Created) == 0) { TrafficPriority.RemoveVehicleFromSegments(vehicleId); return; } if (vehicleData.Info.m_vehicleType != VehicleInfo.VehicleType.Car && vehicleData.Info.m_vehicleType != VehicleInfo.VehicleType.Train && vehicleData.Info.m_vehicleType != VehicleInfo.VehicleType.Tram) { //Log._Debug($"HandleVehicle does not handle vehicles of type {vehicleData.Info.m_vehicleType}"); return; } #if DEBUGV logBuffer.Add("Calculating prio info for vehicleId " + vehicleId); #endif ExtVehicleType?vehicleType = CustomVehicleAI.DetermineVehicleTypeFromVehicle(vehicleId, ref vehicleData); if (vehicleType == null) { Log.Warning($"Could not determine vehicle type of vehicle {vehicleId}!"); } if (vehicleType == null || vehicleType == ExtVehicleType.None) { return; } // add vehicle to our vehicle list VehiclePosition vehiclePos = TrafficPriority.GetVehiclePosition(vehicleId); // we extract the segment information directly from the vehicle var currentPathUnitId = vehicleData.m_path; List <ushort> realTimeDestinationNodes = new List <ushort>(); // current and upcoming node ids List <PathUnit.Position> realTimePositions = new List <PathUnit.Position>(); // current and upcoming vehicle positions #if DEBUGV logBuffer.Add("* vehicleId " + vehicleId + ". currentPathId: " + currentPathUnitId + " pathPositionIndex: " + vehicleData.m_pathPositionIndex); #endif if (currentPathUnitId > 0) { // vehicle has a path... if ((Singleton <PathManager> .instance.m_pathUnits.m_buffer[currentPathUnitId].m_pathFindFlags & PathUnit.FLAG_READY) != 0) { // The path(unit) is established and is ready for use: get the vehicle's current position in terms of segment and lane realTimePositions.Add(Singleton <PathManager> .instance.m_pathUnits.m_buffer[currentPathUnitId].GetPosition(vehicleData.m_pathPositionIndex >> 1)); if (realTimePositions[0].m_offset == 0) { realTimeDestinationNodes.Add(netManager.m_segments.m_buffer[realTimePositions[0].m_segment].m_startNode); } else { realTimeDestinationNodes.Add(netManager.m_segments.m_buffer[realTimePositions[0].m_segment].m_endNode); } if (maxUpcomingPathPositions > 0) { // evaluate upcoming path units byte i = 0; uint pathUnitId = currentPathUnitId; int pathPos = (byte)((vehicleData.m_pathPositionIndex >> 1) + 1); while (true) { if (pathPos > 11) { // go to next path unit pathPos = 0; pathUnitId = Singleton <PathManager> .instance.m_pathUnits.m_buffer[pathUnitId].m_nextPathUnit; #if DEBUGV logBuffer.Add("* vehicleId " + vehicleId + ". Going to next path unit (1). pathUnitId=" + pathUnitId); #endif if (pathUnitId <= 0) { break; } } PathUnit.Position nextRealTimePosition = default(PathUnit.Position); if (!Singleton <PathManager> .instance.m_pathUnits.m_buffer[pathUnitId].GetPosition(pathPos, out nextRealTimePosition)) // if this returns false, there is no next path unit { #if DEBUGV logBuffer.Add("* vehicleId " + vehicleId + ". No next path unit! pathPos=" + pathPos + ", pathUnitId=" + pathUnitId); #endif break; } ushort destNodeId = 0; if (nextRealTimePosition.m_segment > 0) { if (nextRealTimePosition.m_offset == 0) { destNodeId = netManager.m_segments.m_buffer[nextRealTimePosition.m_segment].m_startNode; } else { destNodeId = netManager.m_segments.m_buffer[nextRealTimePosition.m_segment].m_endNode; } } #if DEBUGV logBuffer.Add("* vehicleId " + vehicleId + ". Next path unit! node " + destNodeId + ", seg. " + nextRealTimePosition.m_segment + ", pathUnitId=" + pathUnitId + ", pathPos: " + pathPos); #endif realTimePositions.Add(nextRealTimePosition); realTimeDestinationNodes.Add(destNodeId); if (i >= maxUpcomingPathPositions - 1) { break; // we calculate up to 2 upcoming path units at the moment } ++pathPos; ++i; } } // please don't ask why we use "m_pathPositionIndex >> 1" (which equals to "m_pathPositionIndex / 2") here (Though it would // be interesting to know why they used such an ugly indexing scheme!!). I assume the oddness of m_pathPositionIndex relates // to the car's position on the segment. If it is even the car might be in the segment's first half and if it is odd, it might // be in the segment's second half. #if DEBUGV logBuffer.Add("* vehicleId " + vehicleId + ". *INFO* rtPos.seg=" + realTimePositions[0].m_segment + " nrtPos.seg=" + (realTimePositions.Count > 1 ? "" + realTimePositions[1].m_segment : "n/a")); #endif } } // we have seen the car! vehiclePos.LastFrame = Singleton <SimulationManager> .instance.m_currentFrameIndex; #if DEBUGV logBuffer.Add("* vehicleId " + vehicleId + ". ToNode: " + vehiclePos.ToNode + ". FromSegment: " + vehiclePos.FromSegment /* + ". FromLaneId: " + TrafficPriority.Vehicles[vehicleId].FromLaneId*/); #endif if (addTraffic && vehicleData.m_leadingVehicle == 0 && realTimePositions.Count > 0) { // add traffic to lane uint laneId = PathManager.GetLaneID(realTimePositions[0]); CustomRoadAI.AddTraffic(laneId, (ushort)Mathf.RoundToInt(vehicleData.CalculateTotalLength(vehicleId)), (ushort)Mathf.RoundToInt(lastFrameData.m_velocity.magnitude), realTraffic); } #if DEBUGV logBuffer.Add("* vehicleId " + vehicleId + ". Real time positions: " + realTimePositions.Count + ", Destination nodes: " + realTimeDestinationNodes.Count); #endif if (realTimePositions.Count >= 1) { // we found a valid path unit var sourceLaneIndex = realTimePositions[0].m_lane; if ( !vehiclePos.Valid || vehiclePos.ToNode != realTimeDestinationNodes[0] || vehiclePos.FromSegment != realTimePositions[0].m_segment || vehiclePos.FromLaneIndex != sourceLaneIndex) { // vehicle information is not up-to-date. remove the car from old priority segments (if existing)... TrafficPriority.RemoveVehicleFromSegments(vehicleId); if (realTimePositions.Count >= 2) { // save vehicle information for priority rule handling vehiclePos.Valid = true; vehiclePos.CarState = VehicleJunctionTransitState.None; vehiclePos.WaitTime = 0; vehiclePos.Stopped = false; vehiclePos.ToNode = realTimeDestinationNodes[0]; vehiclePos.FromSegment = realTimePositions[0].m_segment; vehiclePos.FromLaneIndex = realTimePositions[0].m_lane; vehiclePos.ToSegment = realTimePositions[1].m_segment; vehiclePos.ToLaneIndex = realTimePositions[1].m_lane; vehiclePos.ReduceSpeedByValueToYield = UnityEngine.Random.Range(16f, 28f); vehiclePos.OnEmergency = (vehicleData.m_flags & Vehicle.Flags.Emergency2) != 0; vehiclePos.VehicleType = (ExtVehicleType)vehicleType; #if DEBUGV logBuffer.Add($"* vehicleId {vehicleId}. Setting current position to: from {vehiclePos.FromSegment} (lane {vehiclePos.FromLaneIndex}), going over {vehiclePos.ToNode}, to {vehiclePos.ToSegment} (lane {vehiclePos.ToLaneIndex})"); #endif //if (!Options.disableSomething) { // add the vehicle to upcoming priority segments that have timed traffic lights for (int i = 0; i < realTimePositions.Count - 1; ++i) { var prioritySegment = TrafficPriority.GetPrioritySegment(realTimeDestinationNodes[i], realTimePositions[i].m_segment); if (prioritySegment == null) { continue; } // add upcoming segments only if there is a timed traffic light TrafficLightSimulation nodeSim = TrafficLightSimulation.GetNodeSimulation(realTimeDestinationNodes[i]); if (i > 0 && (nodeSim == null || !nodeSim.IsTimedLight() || !nodeSim.IsTimedLightActive())) { continue; } VehiclePosition upcomingVehiclePos = new VehiclePosition(); upcomingVehiclePos.Valid = true; upcomingVehiclePos.CarState = VehicleJunctionTransitState.None; upcomingVehiclePos.LastFrame = vehiclePos.LastFrame; upcomingVehiclePos.ToNode = realTimeDestinationNodes[i]; upcomingVehiclePos.FromSegment = realTimePositions[i].m_segment; upcomingVehiclePos.FromLaneIndex = realTimePositions[i].m_lane; upcomingVehiclePos.ToSegment = realTimePositions[i + 1].m_segment; upcomingVehiclePos.ToLaneIndex = realTimePositions[i + 1].m_lane; upcomingVehiclePos.ReduceSpeedByValueToYield = UnityEngine.Random.Range(16f, 28f); upcomingVehiclePos.OnEmergency = (vehicleData.m_flags & Vehicle.Flags.Emergency2) != 0; upcomingVehiclePos.VehicleType = (ExtVehicleType)vehicleType; #if DEBUGV logBuffer.Add($"* vehicleId {vehicleId}. Adding future position: from {upcomingVehiclePos.FromSegment} (lane {upcomingVehiclePos.FromLaneIndex}), going over {upcomingVehiclePos.ToNode}, to {upcomingVehiclePos.ToSegment} (lane {upcomingVehiclePos.ToLaneIndex})"); #endif prioritySegment.AddVehicle(vehicleId, upcomingVehiclePos); } } //} } else { #if DEBUGV logBuffer.Add($"* vehicleId {vehicleId}. Nothing has changed. from {vehiclePos.FromSegment} (lane {vehiclePos.FromLaneIndex}), going over {vehiclePos.ToNode}, to {vehiclePos.ToSegment} (lane {vehiclePos.ToLaneIndex})"); logme = false; #endif } } else { #if DEBUGV logBuffer.Add($"* vehicleId {vehicleId}. Insufficient path unit positions."); #endif TrafficPriority.RemoveVehicleFromSegments(vehicleId); } #if DEBUGV if (logme) { Log._Debug("vehicleId: " + vehicleId + " ============================================"); foreach (String logBuf in logBuffer) { Log._Debug(logBuf); } Log._Debug("vehicleId: " + vehicleId + " ============================================"); } #endif }
public void Destroy(ref TrafficLightSimulation sim) { DestroyTimedTrafficLight(ref sim); DestroyManualTrafficLight(ref sim); }