public void FindPath3Test() { var points = AStarPathSearch.FindPath(_openMap3, _startPosition3, _targetPosition3); Assert.AreEqual(16, points.Count); Assert.AreEqual(_targetPosition3.X, points[points.Count - 1].X); Assert.AreEqual(_targetPosition3.Y, points[points.Count - 1].Y); }
public void FindPath2Test() { var points = AStarPathSearch.FindPath(_openMap2, _startPosition2, _targetPosition2); Assert.AreEqual(7, points.Count); Assert.AreEqual(_targetPosition2.X, points[points.Count - 1].X); Assert.AreEqual(_targetPosition2.Y, points[points.Count - 1].Y); }
public static TerrainType[] GenerateRoads(TerrainType[] originalMap, RoadGeneratorParameters param) { grid = originalMap; parameters = param; int attemptCounter = 0; for (int road = 0; road < param.numberOfRoads; road++) { Point startPoint; Point endPoint; do { //Select Start do { attemptCounter++; startPoint = Point.GetRandomPoint(); } while (!CheckStart(startPoint) && attemptCounter < MAX_ATTEMPTS); if (attemptCounter >= MAX_ATTEMPTS) { Debug.LogWarning("RoadGen: No suitable start found!"); return(originalMap); } else { attemptCounter = 0; } //Select End do { attemptCounter++; endPoint = Point.GetRandomPoint(); } while (!CheckEnd(endPoint) && attemptCounter < MAX_ATTEMPTS); if (attemptCounter >= MAX_ATTEMPTS) { Debug.LogWarning("RoadGen: No suitable end found!"); return(originalMap); } } while (Utility.ManhattanDistance(startPoint, endPoint) < param.minimumDistance); Debug.Log("Start " + startPoint.ToString() + " End " + endPoint.ToString() + " MHD: " + Utility.ManhattanDistance(startPoint, endPoint)); //Find Path with A* AStarPathSearch.ExternalCostFactor CostFactor = GetExternalCostMethod(param.externalCostFactorMethod); AStarPathSearch.EarlyExitCondition EarlyExit = GetEarlyExitCondition(param.earlyExit); List <Point> path = new List <Point>(); path = AStarPathSearch.FindPath(startPoint, endPoint, IsTraversableTerrain, CostFactor, EarlyExit); //Set desired terraintype of path on grid if (path.Count > 0) { Debug.Log("Path found!"); foreach (Point p in path) { grid[p.gridIndex] = param.desiredRoadTerrain; } } } return(grid); }
public void StartMovement(Tile targetTile) { AreaHighlight(false, currentMovementRange); GameManager.Instance.GetTile(coordinates).OnUnitExit(); targetTile.OnUnitEnter(this); GameManager.Instance.playerCanInteract = false; List <Point> path = AStarPathSearch.FindPath(coordinates, targetTile.coordinates, CanTraversePoint); path.Reverse(); coordinates = targetTile.coordinates; StartCoroutine(LerpThroughPath(path)); }
private void UpdateGame(float deltaTime) { foreach (var unit in _units.Values) { unit.Update(deltaTime); } UpdateMap(); foreach (var unit in _units.Values) { if (unit.Unit.Position == unit.Unit.TargetPosition || unit.Position != unit.Target) { continue; } if (unit.Path == null || unit.Path.Any(p => p != unit.Unit.Position && _map[p.X, p.Y] != 0)) { var path = AStarPathSearch.FindPath(_map, unit.Unit.Position, unit.Unit.TargetPosition); if (path != null) { unit.Path = path.GetRange(1, path.Count - 1); } else { var pathToNearestPoint = AStarPathSearch.FindPathToNearestPoint(_map, unit.Unit.Position, unit.OriginTarget); if (pathToNearestPoint.Count == 1 && pathToNearestPoint[0] == unit.Unit.Position) { unit.Unit.TargetPosition = unit.Unit.Position; continue; } unit.Path = pathToNearestPoint.GetRange(1, pathToNearestPoint.Count - 1); unit.Unit.TargetPosition = unit.Path[unit.Path.Count - 1]; } } if (unit.Path.Count == 0) { continue; } _map[unit.Unit.Position.X, unit.Unit.Position.Y] = 0; unit.Unit.Position = unit.Path.First(p => p != unit.Unit.Position); _map[unit.Unit.Position.X, unit.Unit.Position.Y] = 1; unit.Path.Remove(unit.Unit.Position); unit.SetTarget(new PositionF(unit.Unit.Position.X * CellSize, unit.Unit.Position.Y * CellSize)); } }
public void FindPathFaill2Test() { var points = AStarPathSearch.FindPath(_closeMap2, _startPosition, _targetPosition); Assert.Null(points); }
private void PrepareDataForSimplexMethod() { // TODO: Algorithm: // - Standartization of points // - Calculate hashes for tiles // - Get graphs by hashes or real time add tiles // - Combine graphs // - Path finding // - Convert data for simplex method // TODO: UI usability structure help.Content = "Right click on map to select point"; double[] constraints = new double[pointsList.Count / countOfSources]; for (int i = 0; i < constraints.Length; i++) { constraints[i] = Convert.ToInt32(Microsoft.VisualBasic.Interaction.InputBox($"Cost condition for {i}:", "Please, enter the field", "1")); } double[] simplex = new double[countOfSources]; for (int i = 0; i < simplex.Length; i++) { simplex[i] = Convert.ToInt32(Microsoft.VisualBasic.Interaction.InputBox($"Satisfaction condition for {i}:", "Please, enter the field", "1")); } double cZoom = dataController.ConvertToMapZoom(zoom); Vector2 <double> tile = MercatorProjection.LatLngToTile(pointsList.First().TileLatLng, cZoom).Floor(); Graph graph = dataController.GetRoads(new Vector3 <double>(tile.X, tile.Y, cZoom)); if (graph == null) { return; } //List<string> hashes = pointsList.GetMapTileHashes(dataController.ConvertToMapZoom(zoom)); //Graph.CombineGraphs(dataController.GetRoads(hashes[0]).nodes, dataController.GetRoads(hashes[1]).nodes); List <Node> points = new List <Node>(); foreach (RaycastMapResult point in pointsList) { //var proj = MercatorProjection.LatLngToTile(, cZoom); var proj = point.PointLatLng; points.Add(graph.FindNearest(proj)); } Console.WriteLine("Clear list of points..."); pointsList.Clear(); double[,] distances = new double[points.Count / countOfSources, countOfSources]; int counterX = 0; int counterY = 0; for (int i = 1; i < points.Count; i++) { if (counterX == countOfSources) { counterY = 0; counterX++; } AStarPathSearch.AStarPathSearchResult result = AStarPathSearch.FindPath(points[0], points[i]); if (result == null) { return; } distances[counterX, counterY] = result.Score; counterY++; } Matrix <double> distanceMatrix = DenseMatrix.OfArray(distances); Vector <double> constraintsVector = DenseVector.OfArray(constraints); Vector <double> simplexFunction = DenseVector.OfArray(simplex); BasisFinder basisFinder = new BasisFinder(distanceMatrix, simplexFunction, constraintsVector); int[] basis = basisFinder.GetBasis(); Console.WriteLine($"Basis: {string.Join(", ", basis)}"); Simplex simplexMethod = new Simplex(basisFinder, basis); simplexMethod.FindAnswer(); Console.WriteLine($"Matrix: {simplexMethod.matrix.ToString()}"); Console.WriteLine($"Constraints: {constraintsVector.ToString()}"); Console.WriteLine($"Simplex function: {simplexFunction.ToString()}"); double answer = simplexMethod.GetAnswer(); Console.WriteLine($"Answer: {answer}"); }