void Start() { string wd = Directory.GetCurrentDirectory(); print("wd=" + wd); readConfig(); roadLine = GetComponent <RoadLine>(); startPoint = roadLine.startPoint.name; endPoint = roadLine.endPoint.name; }
private void _startTrafficFlow_Click(object sender, EventArgs e) { _isTrafficFlowConfigure = true; _currentTrafficFlow = new TrafficFlow(); var roadLine = new RoadLine { Color = GetNewPen() }; _trafficFlowToRoadLineDictionary.Add(_currentTrafficFlow, roadLine); }
void Start() { roadLine = GameObject.FindObjectOfType <RoadLine>(); if (drawDebugDir) { lineRenderer = GetComponent <LineRenderer>(); lineRenderer.SetColors(Color.green, Color.green); } }
public GraphNode GetConnectedToByLine(RoadLine line) { for (int i = 0; i < ConnectedRoads.Count; i++) { if (ConnectedRoads[i].Value == line) { return(ConnectedRoads[i].ConnectedTo); } } return(null); }
private Pen GetNewPen() { foreach (var availableRoadLineColor in _availableRoadLineColors) { var roadLine = new RoadLine { Color = availableRoadLineColor }; if (!_trafficFlowToRoadLineDictionary.ContainsValue(roadLine)) { return(availableRoadLineColor); } } var random = new Random(); return(new Pen(Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)))); }
public RoadLine[] GenerateRoadNodes(ERRoad road, Road roadLines, float nodeDefinition, int definition, int count, int linesCount, bool invertFirst, bool oneWay) { RoadLine[] lines = new RoadLine[linesCount];//(count*(linesCount/2)); float plusMinus = 1f; int elCount = (int)(road.roadScript.distances[road.roadScript.distances.Count - 1] / nodeDefinition); if (elCount == 0) { elCount = 2; } if ((float)elCount % 2 != 0f) { elCount += 1; } for (int i = 0; i < linesCount; i++) { GameObject tempGo = new GameObject("Line" + (i), typeof(RoadLine)); tempGo.transform.SetParent(roadLines.transform); lines[i] = (tempGo.GetComponent <RoadLine>()); lines[i].GraphNodes = new List <GraphNode>(elCount / linesCount); lines[i].road = roadLines; plusMinus *= -1f; lines[i].GraphNodes.AddRange(GenerateOneLine(road, roadLines, (linesCount > 1 ? (road.roadScript.roadWidth / 2f) : 0f) * plusMinus, definition, elCount, oneWay ? !invertFirst : (plusMinus < 0f))); if (plusMinus > 0f) { plusMinus += (road.roadScript.roadWidth / 4f); } } for (int i = 0; i < linesCount; i++) { for (int j = 0; j < lines[i].GraphNodes.Count; j++) { lines[i].GraphNodes[j].Transform.SetParent(lines[i].transform); } } return(lines); }
public List <Link> GetConnectedRoadLines(Road road, RoadLine line, List <Link> connectedLines, GraphNode node) { for (int i = 0; i < node.ConnectedNodes.Count; i++) { if (road.GetRoadLineByNode(node.ConnectedNodes[i]) != line) { Road tempRoad = GlobalAINavigator.GetRoadByNode(node, node.ConnectedNodes[i]); if (tempRoad != null) { RoadLine tempLine = tempRoad.GetRoadLineByNode(node.ConnectedNodes[i]); if (tempLine != null) { connectedLines.Add(new Link(node, node.ConnectedNodes[i], tempLine)); } } } } return(connectedLines); }
private void _tsmiOpenConfig_Click(object sender, EventArgs e) { var dialog = new OpenFileDialog { Filter = "XML file (*.xml)|*.xml", RestoreDirectory = true, DereferenceLinks = false, AutoUpgradeEnabled = false }; if (dialog.ShowDialog() != DialogResult.OK) { return; } _map.ClearElements(); _workField.Controls.Clear(); _trafficManager.TrafficFlows.Clear(); var xmlDocument = new XmlDocument(); xmlDocument.Load(dialog.FileName); var configList = xmlDocument.SelectNodes("Config"); if (configList.Count != 1) { throw new Exception("Invalid config file structure"); } var mapList = configList[0].SelectNodes("Map"); if (mapList.Count != 1) { throw new Exception("Invalid config file structure"); } var elementList = mapList[0].SelectNodes("MapElement"); for (var elementIter = 0; elementIter < elementList.Count; elementIter++) { var mapElementAttributes = elementList[elementIter].Attributes; var row = int.Parse(mapElementAttributes["Row"].Value); var column = int.Parse(mapElementAttributes["Column"].Value); var type = mapElementAttributes["Type"].Value; if (type == "Crossroad") { AddCrossroad(row, column); } else if (type == "Road") { var orientation = mapElementAttributes["Orientation"].Value; if (orientation == "Horizontal") { AddHorizontalRoad(row, column); } else if (orientation == "Vertical") { AddVerticalRoad(row, column); } else { throw new ArgumentException("Invalid road orientation."); } } else if (type == "Turn") { var orientation = mapElementAttributes["Orientation"].Value; if (orientation == "DownToLeft") { AddDownToLeftTurn(row, column); } else if (orientation == "LeftToUp") { AddLeftToUpTurn(row, column); } else if (orientation == "RightToDown") { AddRightToDownTurn(row, column); } else if (orientation == "UpToRight") { AddUpToRightTurn(row, column); } else { throw new ArgumentException("Invalid turn orientation."); } } else { throw new ArgumentException("Unknown element type."); } } var trafficFlowsList = configList[0].SelectNodes("TrafficFlows"); if (trafficFlowsList.Count != 1) { throw new Exception("Invalid config file structure"); } var trafficFlowList = trafficFlowsList[0].SelectNodes("TrafficFlow"); for (var trafficFlowIter = 0; trafficFlowIter < trafficFlowList.Count; trafficFlowIter++) { _currentTrafficFlow = new TrafficFlow(); var roadLine = new RoadLine { Color = GetNewPen() }; _trafficFlowToRoadLineDictionary.Add(_currentTrafficFlow, roadLine); var trafficFlowAttributes = trafficFlowList[trafficFlowIter].Attributes; var density = double.Parse(trafficFlowAttributes["Density"].Value, CultureInfo.InvariantCulture); var speed = double.Parse(trafficFlowAttributes["Speed"].Value, CultureInfo.InvariantCulture); var pathList = trafficFlowList[trafficFlowIter].SelectNodes("Path"); if (pathList.Count != 1) { throw new Exception("Invalid config file structure"); } var locationList = pathList[0].SelectNodes("Location"); for (var locationIter = 0; locationIter < locationList.Count; locationIter++) { var locationAttributes = locationList[locationIter].Attributes; var locationRow = int.Parse(locationAttributes["Row"].Value, CultureInfo.InvariantCulture); var locationColumn = int.Parse(locationAttributes["Column"].Value, CultureInfo.InvariantCulture); _currentTrafficFlow.Path.Add(new Location(locationRow, locationColumn)); } foreach (var location in _currentTrafficFlow.Path) { var control = LocationToControl(location) as ACrossroadControl; if (control == null) { throw new Exception("Something went wrong..."); } control.DeleteLine(roadLine); control.AddLine(roadLine); control.Invalidate(); } _currentTrafficFlow.TrafficSpeed = speed; _currentTrafficFlow.TrafficDensity = density; _trafficManager.AddTrafficFlow(_currentTrafficFlow); } }
public Link(GraphNode key, GraphNode connectedTo, RoadLine value) { Key = key; Value = value; ConnectedTo = connectedTo; }
public List <INode> GetRouteToNode(INode start, INode end) { List <INode> nodePath = new List <INode>(); List <RoadLine> nextRoads = new List <RoadLine>(); bool enough = false; Road startNodeRoad = GetRoadByNode(start, start); Road endNodeRoad = GetRoadByNode(start, end); if (startNodeRoad == null) { startNodeRoad = GetRoadByNode(start, start, true); } if (endNodeRoad == null) { endNodeRoad = GetRoadByNode(start, end, true); } //Debug.Log(startNodeRoad + " " + start + " -> " + endNodeRoad + " " + end); RoadLine startNodeLine = startNodeRoad.GetRoadLineByNode(start); RoadLine endNodeLine = endNodeRoad.GetRoadLineByNode(end); if (startNodeLine == null) { startNodeLine = startNodeRoad.GetRoadLineByNode(start, true); } if (endNodeLine == null) { endNodeLine = endNodeRoad.GetRoadLineByNode(end, true); } Dictionary <RoadLine, float> Weights = new Dictionary <RoadLine, float>(); Dictionary <RoadLine, List <RoadLine> > Paths = new Dictionary <RoadLine, List <RoadLine> >(); Paths.Add(startNodeLine, new List <RoadLine>()); //Paths[startNodeLine].Add(startNodeLine); List <RoadLine> Marked = new List <RoadLine>(); RoadLine currentRoadLine = startNodeLine; Weights.Add(currentRoadLine, 0f); // алгоритм - строим по Дейкстре кратчайший путь, используя за логические узлы - RoadLine // далее по RoadLine в зависимости от пути собираем список нод и возвращаем while (!enough) { if (!Marked.Contains(currentRoadLine)) { Marked.Add(currentRoadLine); } for (int i = 0; i < currentRoadLine.ConnectedRoads.Count; i++) { RoadLine line = currentRoadLine.ConnectedRoads[i].Value; if (line == null) { Debug.Log(currentRoadLine.road.transform.name + " " + i); } if (!Weights.ContainsKey(line)) { Weights.Add(line, float.MaxValue); } float distanceToInters = currentRoadLine.GetDistanceForIntersectionNode(currentRoadLine.GetNodeForConnectedRoadLine(line)); if ((distanceToInters + Weights[currentRoadLine]) < Weights[line]) { if (line != currentRoadLine) { Weights[line] = distanceToInters + Weights[currentRoadLine]; if (Paths.ContainsKey(line)) { Paths[line].Clear(); } else { Paths.Add(line, new List <RoadLine>()); } if (Marked.Contains(line)) { Marked.Remove(line); } if (!nextRoads.Contains(line)) { nextRoads.Add(line); } Paths[line].AddRange(Paths[currentRoadLine]); Paths[line].Add(currentRoadLine); } } } RoadLine tempCurrentRoadLine = currentRoadLine;//currentRoadLine.ConnectedRoads[0].Value; float min = float.MaxValue; for (int i = 0; i < currentRoadLine.ConnectedRoads.Count; i++) { if (Weights[currentRoadLine.ConnectedRoads[i].Value] < min && !Marked.Contains(currentRoadLine.ConnectedRoads[i].Value)) { min = Weights[currentRoadLine.ConnectedRoads[i].Value]; tempCurrentRoadLine = currentRoadLine.ConnectedRoads[i].Value; } } if (currentRoadLine != tempCurrentRoadLine && currentRoadLine != endNodeLine) { currentRoadLine = tempCurrentRoadLine; } else { for (int i = 0; i < currentRoadLine.ConnectedRoads.Count; i++) { if (currentRoadLine.ConnectedRoads[i].Value != tempCurrentRoadLine && !Marked.Contains(currentRoadLine.ConnectedRoads[i].Value)) { if (!nextRoads.Contains(currentRoadLine.ConnectedRoads[i].Value)) { nextRoads.Add(currentRoadLine.ConnectedRoads[i].Value); } } } if (nextRoads.Count > 0) { tempCurrentRoadLine = nextRoads.FirstOrDefault(); nextRoads.Remove(tempCurrentRoadLine); currentRoadLine = tempCurrentRoadLine; } else { enough = true; } } } if (Paths.ContainsKey(endNodeLine)) { Paths[endNodeLine].Add(endNodeLine); List <RoadLine> path = Paths[endNodeLine]; int endIndex, startIndex; for (int i = 0; i < path.Count; i++) { if (path[i] == startNodeLine) { if (path.Count > 1) { endIndex = path[i].GetIndexOfNode(path[i].GetNodeForConnectedRoadLine(path[i + 1])); } else { endIndex = path[i].GetIndexOfNode((GraphNode)end); } startIndex = path[i].GetIndexOfNode((GraphNode)start); if (startIndex == -1) { for (int j = 0; j < start.GetConnectedNodes().Count; j++) { if (GetRoadByNode(start, start.GetConnectedNodes()[j]) == path[i]) { startIndex = path[i].GetIndexOfNode(start.GetConnectedNodes()[j]); } } } if (startIndex == -1) { startIndex = 0; } // start 29 end 6 if (startIndex > endIndex) { int temp = startIndex; //29 startIndex = (path[i].GraphNodes.Count - 1) - startIndex; //29-29=0 endIndex = temp - endIndex - 1; // 29 - 6 = 23 Road road = path[i].road; for (int j = 0; j < road.Lines.Count; j++) { if (road.Lines[j] != path[i]) { path[i] = road.Lines[j]; break; } } } } else if (path[i] == endNodeLine) { endIndex = path[i].GetIndexOfNode((GraphNode)end); if (path.Count > 1) { startIndex = path[i].GetIndexOfNode(path[i - 1].GetConnectedToByLine(path[i])); } else { startIndex = path[i].GetIndexOfNode((GraphNode)start); } if (endIndex == -1) { for (int j = 0; j < path[i].RoadIntersectionsIndexes.Count; j++) { for (int k = 0; k < path[i].GraphNodes[path[i].RoadIntersectionsIndexes[j]].ConnectedNodes.Count; k++) { if (path[i].GraphNodes[path[i].RoadIntersectionsIndexes[j]].ConnectedNodes[k] == end) { endIndex = path[i].RoadIntersectionsIndexes[j]; } } } } if (endIndex == -1) { endIndex = path[i].GraphNodes.Count - 1; } } else { endIndex = path[i].GetIndexOfNode(path[i].GetNodeForConnectedRoadLine(path[i + 1])); startIndex = path[i].GetIndexOfNode(path[i - 1].GetConnectedToByLine(path[i])); } for (int j = startIndex; j <= endIndex; j++) { nodePath.Add(path[i].GraphNodes[j]); } } //nodePath.Add(end); } else { //Debug.LogError("Failed to build path =("); } return(nodePath); }