示例#1
0
        public void FindPathToNearestPoint1Test()
        {
            var points = AStarPathSearch.FindPathToNearestPoint(_closeMap1, _startPosition, _targetPosition);

            Assert.NotNull(points);
            Assert.AreEqual(5, points.Count);
        }
示例#2
0
        public void GetPathForNodeTest()
        {
            List <Position> positions = new List <Position>
            {
                new Position(0, 0),
                new Position(0, 1),
                new Position(0, 2)
            };

            PathNode pathNode = new PathNode
            {
                Position    = positions[2],
                ParentPoint = new PathNode
                {
                    Position    = positions[1],
                    ParentPoint = new PathNode()
                    {
                        Position = positions[0]
                    }
                }
            };

            var path = AStarPathSearch.GetPathForNode(pathNode);

            Assert.AreEqual(positions.Count, path.Count);
            Assert.AreEqual(positions, path);
        }
示例#3
0
        public void GetNearestPoint1Test()
        {
            var points = AStarPathSearch.GetNearestPoints(_targetPosition, _openMap, 1);

            Assert.NotNull(points);
            Assert.AreEqual(3, points.Count);
        }
示例#4
0
        public void GetNearestPoint3Test()
        {
            var points = AStarPathSearch.GetNearestPoints(_targetPosition, _closeMap1, 2);

            Assert.NotNull(points);
            Assert.AreEqual(5, points.Count);
        }
示例#5
0
        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);
        }
示例#6
0
        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);
        }
示例#7
0
        public void GetHeuristicPathLengthTest()
        {
            var startPoint = new Position(0, 0);
            var endPoint   = new Position(5, 10);

            int len = AStarPathSearch.GetHeuristicPathLength(startPoint, endPoint);

            Assert.AreEqual(15, len);
        }
    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);
    }
示例#9
0
        public void GetDistanceBetweenNeighborsTest()
        {
            var startPoint = new Position(1, 1);
            var endPoint1  = new Position(1, 0);
            var endPoint2  = new Position(1, 2);

            int len1 = AStarPathSearch.GetDistanceBetweenNeighbors(startPoint, endPoint1);
            int len2 = AStarPathSearch.GetDistanceBetweenNeighbors(startPoint, endPoint2);

            Assert.AreEqual(1, len1);
            Assert.AreEqual(1, len2);
        }
示例#10
0
    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));
    }
示例#11
0
        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));
            }
        }
示例#12
0
        public void GetNeighborsTest()
        {
            PathNode node = new PathNode
            {
                Position                    = _startPosition,
                ParentPoint                 = null,
                PathLengthFromStart         = 0,
                HeuristicEstimatePathLength = AStarPathSearch.GetHeuristicPathLength(_startPosition, _targetPosition)
            };

            var nodes = AStarPathSearch.GetNeighbors(node, _targetPosition, _openMap);

            Assert.AreEqual(4, nodes.Count);
        }
示例#13
0
        public void FindPathFaill2Test()
        {
            var points = AStarPathSearch.FindPath(_closeMap2, _startPosition, _targetPosition);

            Assert.Null(points);
        }
示例#14
0
        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}");
        }