Exemple #1
0
        private static void DifficultyExample()
        {
            Console.Clear();
            Console.Out.WriteLine("Example with movement penalties in map.");
            WriteLegend();

            var map = new MapSample();

            for (var y = 0; y < map.Height / 3; y++)
            {
                map[map.Width / 3, y].Difficulty = 9;
                map[map.Width / 3, map.Height - 1 - y].Difficulty = 9;
                map[2 * map.Width / 3, y].Difficulty = 3;
                map[2 * map.Width / 3, map.Height - 1 - y].Difficulty = 3;
            }
            Console.Out.Write(map.ToString());
            var pathFinder = new AStarPathfinder(map, 1000, false, new ManhattanDistance());
            var from       = new PointInt32(0, 0);
            var to         = new PointInt32(map.Width - 1, map.Height - 1);

            Console.Out.WriteLine("Path from {0} to {1}.", from, to);
            var path = pathFinder.FindPath(null, from, to);

            Console.Out.Write(map.ToString(path, true));
            Console.Out.WriteLine("Press any key to continue ...");
            Console.In.ReadLine();
        }
	// Use this for initialization
	void Start () {
		//Get the reference to object's AStarPathfinder component
		pathfinder = transform.GetComponent<AStarPathfinder> ();

		//get the "player" as target - transform gives the info we need for this
		targetT = GameObject.FindGameObjectWithTag ("Player").transform;
	}
Exemple #3
0
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            MapCreationScript.Instance.ClearWorld();

            Vector2Int startPos  = new Vector2Int((int)Random.Range(0, 49), (int)Random.Range(0, 49));
            Vector2Int targetPos = new Vector2Int((int)Random.Range(0, 49), (int)Random.Range(0, 49));

            Cell startCell  = world.map.GetCellAt(startPos);
            Cell targetCell = world.map.GetCellAt(targetPos);

            AStarPathfinder aPathfinder = new AStarPathfinder(world.map);

            bool result = aPathfinder.FindPath(startCell, targetCell);

            foreach (ANode node in aPathfinder.visited)
            {
                world.map.GetCellAt(new Vector2Int(node.x, node.y)).GetGameObject().GetComponent <SpriteRenderer>().color = Color.black;
            }

            foreach (ANode node in aPathfinder.path)
            {
                world.map.GetCellAt(new Vector2Int(node.x, node.y)).GetGameObject().GetComponent <SpriteRenderer>().color = Color.red;
            }
        }
    }
 public void GeneratePath()
 {
     AStarPathfinder finder = new AStarPathfinder();
     finder.map = map;
     finder.GeneratePath(spawnPos, targetPos);
     path = finder.Path;
 }
 // Use this for initialization
 void Start()
 {
     //Get the references to object's AStarPathfinder component
     pathfinder = transform.GetComponent <AStarPathfinder> ();
     //Initialise the followPosition to the current position
     followPosition = transform.position;
 }
        public void Search_BackwardPath_PathFound()
        {
            /*
             ##__
             * 10#_
             * 2#6_
             * 345_
             */
            var target = new AstarGridGraph(10, 10);

            target.Walls.Add(new Point(1, 2));
            target.Walls.Add(new Point(2, 1));
            target.Walls.Add(new Point(1, 0));
            target.Walls.Add(new Point(0, 0));
            var result = AStarPathfinder.Search(target, new Point(1, 1), new Point(2, 2));

            Assert.AreEqual(7, result.Count());
            Assert.AreEqual(new Point(1, 1), result[0]);
            Assert.AreEqual(new Point(0, 1), result[1]);
            Assert.AreEqual(new Point(0, 2), result[2]);
            Assert.AreEqual(new Point(0, 3), result[3]);
            Assert.AreEqual(new Point(1, 3), result[4]);
            Assert.AreEqual(new Point(2, 3), result[5]);
            Assert.AreEqual(new Point(2, 2), result[6]);
        }
        public static bool TryFindPath(this LocalPlayerCharacterView instance, AStarPathfinder pathfinder, Vector3 target,
                                       StopFunction <Vector2> stopFunction, out List <Vector3> results)
        {
            results = new List <Vector3>();

            var pivotPoints = new List <Vector2>();
            var path        = new List <Vector2>();

            var startLocation = new Vector2((int)instance.transform.position.x, (int)instance.transform.position.z);
            var endLocation   = new Vector2((int)target.x, (int)target.z);

            var landscape = Landscape.Instance;

            if (pathfinder.TryFindPath(startLocation, endLocation, stopFunction, out path, out pivotPoints, true))
            {
                foreach (var point in path)
                {
                    results.Add(new Vector3(point.x, landscape.GetLandscapeHeight(point.b()) + 0.5f, point.y));
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Exemple #8
0
    public static List <Cell> PathToTarget(this GameMap gm, Cell origin, Cell goal)
    {
        var pathFinder = new AStarPathfinder();

        pathFinder.FindPath(origin, goal, gm.CellGameMap, false);
        return(pathFinder.CellsFromPath());
    }
        public static bool TryFindPath(this LocalPlayerCharacterView instance, AStarPathfinder pathfinder, SimulationObjectView target,
                                       StopFunction <Vector2> stopFunction, out List <Vector3> results)
        {
            results = new List <Vector3>();

            if (instance.TryFindPath(pathfinder, target.transform.position, stopFunction, out results))
            {
                /*
                 * var collider = target.GetComponent<Collider>();
                 *
                 * if (collider != null)
                 * {
                 *      while (collider.bounds.Contains(results.Last()))
                 *              results.RemoveAt(results.Count - 1);
                 *
                 *      var lastNode = results.Last();
                 *      var closestNode = collider.ClosestPointOnBounds(lastNode);
                 *      var direction = (closestNode - lastNode).normalized / 2;
                 *
                 *      results.Add(closestNode - direction);
                 * }
                 *
                 * results.Insert(0, instance.transform.position);
                 */
            }
            else
            {
                return(false);
            }

            return(true);
        }
Exemple #10
0
    // Use this for initialization
    private void Start()
    {
        var a = new Node("A");
        var b = new Node("B");
        var c = new Node("C");
        var d = new Node("D");
        var e = new Node("E");
        var f = new Node("F");

        a.AddOutgoingEdge(new Edge(a, b, 4));
        a.AddOutgoingEdge(new Edge(a, c, 2));
        a.AddOutgoingEdge(new Edge(a, f, 50));
        b.AddOutgoingEdge(new Edge(b, c, 5));
        b.AddOutgoingEdge(new Edge(b, d, 10));
        c.AddOutgoingEdge(new Edge(c, e, 3));
        e.AddOutgoingEdge(new Edge(e, d, 4));
        d.AddOutgoingEdge(new Edge(d, f, 11));

        var pathfinder = new AStarPathfinder <Node>(Heuristic);
        var timer      = new StopwatchExecutionTimer();
        var path       = pathfinder.FindPath(a, f);

        print(string.Format("In {1} seconds found the following path with cost {0} from A to F:", path.Cost, timer.ElapsedSeconds));
        print(path.Edges.Aggregate("", (soFar, edge) => soFar + (soFar.Length > 0 ? " -> " : "") + edge));
    }
Exemple #11
0
        public void MoveToNode(IGraphNode node, int movementLimit = -1)
        {
            // Find the shortest route to the destination node, and start moving towards it.
            var pathfinder = new AStarPathfinder();

            path = pathfinder.FindPath(GetTravelingNode(), node);

            if (OnStartMove != null)
            {
                OnStartMove.Invoke();
            }

            if (movementLimit > -1 && path.nodes.Count > movementLimit)
            {
                path.nodes.RemoveRange(movementLimit, path.nodes.Count - movementLimit);
            }


            if (path != null)
            {
                pathEnumerator = path.nodes.GetEnumerator();
                pathEnumerator.MoveNext();
                SetTargetNode(pathEnumerator.Current, false);
            }
        }
Exemple #12
0
 private void Start()
 {
     LoadModel();
     pathfinder = new AStarPathfinder();
     file       = model;
     InstantiateModel(model);
 }
Exemple #13
0
        public IPath FindPath(Location goal)
        {
            AbstractPathfinder algorithm = new AStarPathfinder();

            _path = algorithm.FindPath(_grid, _position, goal);

            return(_path);
        }
Exemple #14
0
        static void Main(string[] args)
        {
            var map                  = new Map();
            var pathfinder           = new AStarPathfinder((x, y) => map[x, y]);
            List <NodePosition> path = pathfinder.Calculate((0, 0), (2, 4));

            Console.ReadKey();
        }
 //Run on game start
 void Start()
 {
     pathfinder     = transform.GetComponent <AStarPathfinder> ();
     animator       = GetComponent <Animator>();
     rb             = GetComponent <Rigidbody2D> ();
     patrolLocation = transform.position;
     GetComponent <Enemy>().noFlip = false;
 }
 // Use this for initialization
 void Start()
 {
     //chase = false;
     chaseSpeed = 0;
     chaseStop  = false;
     //Get the reference to object's AStarPathfinder component
     pathfinder   = transform.GetComponent <AStarPathfinder> ();
     pathWayPoint = transform.GetComponent <AStarPathfinder>();
 }
Exemple #17
0
 public override bool LoadMapData(Stream stream)
 {
     bool ret = base.LoadMapData(stream);
     if (ret)
     {
         pathFinder = new AStarPathfinder(heightData);
     }
     return ret;
 }
 void AddNeighbourNode(int xOffset, int yOffset, NodePosition parentPos, AStarPathfinder aStarSearch)
 {
     if (ValidNeigbour(xOffset, yOffset) &&
         !(parentPos.x == position.x + xOffset && parentPos.y == position.y + yOffset))
     {
         NodePosition  neighbourPos = new NodePosition(position.x + xOffset, position.y + yOffset);
         MapSearchNode newNode      = pathfinder.AllocateMapSearchNode(neighbourPos);
         aStarSearch.AddSuccessor(newNode);
     }
 }
        public void Day15Part1()
        {
            var repair           = new OxygenRepairDroid(UserMode.LeastExplored);
            var result           = repair.Repair();
            var pathfinder       = new AStarPathfinder();
            var path             = pathfinder.FindPath(result);
            var numberOfCommands = path.Count + 1;

            numberOfCommands.Should().Be(238);
        }
 void Start()
 {
     pathfinder       = transform.GetComponent <AStarPathfinder> ();
     patrolLocation   = transform.position;
     firePoint        = transform.Find("FirePoint");
     animator         = GetComponent <Animator>();
     rb               = GetComponent <Rigidbody2D> ();
     modifierOriginal = gameObject.GetComponent <Enemy>().knockbackModifier;
     GetComponent <Enemy>().noFlip = false;
 }
    // Use this for initialization
    void Start()
    {
        target = GameObject.Find("Player");

        //Get the reference to object's AStarPathfinder component
        pathfinder   = transform.GetComponent <AStarPathfinder>();
        pathWayPoint = transform.GetComponent <AStarPathfinder>();
        if (waypoints.Length == 0)
        {
            waypoints = new Vector3[] { transform.position };
        }
    }
Exemple #22
0
    //counting the number of trigger colliders that the player has passed through in order to get the number of moves they have made/remain.
    //public int NumberOfMoves = 0;
    //public int MovesLeft;

    //getting the player so that you can detect when it hits a trigger collider
    //public GameObject Player;
    //public Collider PlayerCollider;
    public void Start()
    {
        mainCamera = Camera.main;
        path       = this.GetComponent <AStarPathfinder>();

        CreateTiles();

        SearchAdjacentTiles();

        //MovesLeft = NumberOfMoves;

        //PlayerCollider = Player.GetComponent<Collider>();
    }
Exemple #23
0
    int remainingNodeCount;                             // How many nodes have not been put in the closed list
    // Used to ensure that the algorithm does not loop infinitely
    // if there are problems in the code

    void Awake()
    {
        pathfinder = this;                                                      // Initializes this pathfinder as public static object

        // Gets all the nodes in the scene
        nodes = GetComponentsInChildren <AStarNode>();
        for (int n = 0; n < nodes.Length; ++n)
        {
            nodes[n].DebugLinesOn = debugLines;                 // Turns on debug lines for each node (or not)
            nodes[n].GetComponent <MeshRenderer>().enabled = false;
            // Turns off the mesh renderer so world is not filled with white spheres
        }
    }
    void Start()
    {
        blockGrid  = new List <List <GameObject> >();
        statesGrid = new List <List <Block.States> >();

        AStar     = new AStarPathfinder();
        foundPath = new List <List <Block.States> >();

        camera = Camera.main;

        parseMaze();
        setCamera();
        AStar.initializeMaze(statesGrid);
        foundPath = AStar.algorithm();
        updateMaze(foundPath);
    }
    // This generates the successors to the given Node. It uses a helper function called
    // AddSuccessor to give the successors to the AStar class. The A* specific initialisation
    // is done for each node internally, so here you just set the state information that
    // is specific to the application
    public bool GetSuccessors(AStarPathfinder aStarSearch, MapSearchNode parentNode)
    {
        NodePosition parentPos = new NodePosition(0, 0);

        if (parentNode != null)
        {
            parentPos = parentNode.position;
        }

        // push each possible move except allowing the search to go backwards
        AddNeighbourNode(-1, 0, parentPos, aStarSearch);
        AddNeighbourNode(0, -1, parentPos, aStarSearch);
        AddNeighbourNode(1, 0, parentPos, aStarSearch);
        AddNeighbourNode(0, 1, parentPos, aStarSearch);

        return(true);
    }
        public void Search_ForwardPath_PathFound()
        {
            /*
             * ____
             * _01_
             * _#2_
             */
            var target = new AstarGridGraph(10, 10);

            target.Walls.Add(new Point(1, 2));
            var result = AStarPathfinder.Search(target, new Point(1, 1), new Point(2, 2));

            Assert.AreEqual(3, result.Count());
            Assert.AreEqual(new Point(1, 1), result[0]);
            Assert.AreEqual(new Point(2, 1), result[1]);
            Assert.AreEqual(new Point(2, 2), result[2]);
        }
        public void Search_NoWay_PathNull()
        {
            /*
             * _#__
             #0#_
             * _#x_
             * ____
             */
            var target = new AstarGridGraph(10, 10);

            target.Walls.Add(new Point(1, 2));
            target.Walls.Add(new Point(2, 1));
            target.Walls.Add(new Point(1, 0));
            target.Walls.Add(new Point(0, 1));
            var result = AStarPathfinder.Search(target, new Point(1, 1), new Point(2, 2));

            Assert.AreEqual(null, result);
        }
Exemple #28
0
        public void Test1()
        {
            var grid       = new AStarGrid(CreateMap(5, 5));
            var pathfinder = new AStarPathfinder(grid);

            List <Vector2> path = null;

            path = pathfinder.FindPath(0, 0, 1, 1);
            Assert.AreEqual(1, path.Count);

            path = pathfinder.FindPath(0, 0, 2, 2);
            Assert.AreEqual(2, path.Count);

            path = pathfinder.FindPath(0, 0, 2, 1);
            Assert.AreEqual(2, path.Count);

            path = pathfinder.FindPath(0, 0, 3, 2);
            Assert.AreEqual(3, path.Count);
        }
Exemple #29
0
        private static void SimpleExample()
        {
            Console.Clear();
            Console.Out.WriteLine("Simple example - empty map.");
            WriteLegend();

            var map = new MapSample();

            Console.Out.Write(map.ToString());
            var pathFinder = new AStarPathfinder(map, 1000, false, new ManhattanDistance());
            var from       = new PointInt32(0, 0);
            var to         = new PointInt32(map.Width - 1, map.Height - 1);

            Console.Out.WriteLine("Path from {0} to {1}.", from, to);
            var path = pathFinder.FindPath(null, from, to);

            Console.Out.Write(map.ToString(path, true));
            Console.Out.WriteLine("Press any key to continue ...");
            Console.In.ReadLine();
        }
        public void Search_AllowDiagonal_PathFound()
        {
            /*
             ##__
             * _0#_
             * _#1_
             * ____
             */
            var target = new AstarGridGraph(10, 10, true);

            target.Walls.Add(new Point(1, 2));
            target.Walls.Add(new Point(2, 1));
            target.Walls.Add(new Point(1, 0));
            target.Walls.Add(new Point(0, 0));
            var result = AStarPathfinder.Search(target, new Point(1, 1), new Point(2, 2));

            Assert.AreEqual(2, result.Count());
            Assert.AreEqual(new Point(1, 1), result[0]);
            Assert.AreEqual(new Point(2, 2), result[1]);
        }
Exemple #31
0
    private void RenderTiles(AStarPathfinder pathfinder)
    {
        MapSearchNode node = pathfinder.GetSolutionStart();

        for ( ;;)
        {
            node = pathfinder.GetSolutionNext();

            if (node == null)
            {
                break;
            }

            GameObject obj = objs[node.position.x, node.position.y];
            if (obj != null)
            {
                obj.GetComponent <MeshRenderer>().material.color = Color.green;
            }
        }
        ;
    }
Exemple #32
0
        public ScenarioState(GameRoot game, GraphicsDevice graphicsDevice, ScenarioWrapper scenario)
            : base(game, graphicsDevice)
        {
            // Load grid
            Tuple <bool, Rectangle> previewInfo = Tuple.Create(false, Rectangle.Empty);

            scenarioGrid = new Grid(previewInfo, 50, scenario);

            // Load pathfinder class
            pathfinder = new AStarPathfinder(scenarioGrid);
            AStarPathfinder.AllowDiagonalMovement = true;
            AStarPathfinder.InstantPathing        = false;
            pathfinder.PathfindingFinished       += PathfindingFinished;

            // Configure grid
            scenarioGrid.AssignNewAlgorithm(pathfinder);
            scenarioGrid.NodeHovered += ScenarioGrid_NodeHovered;
            scenarioGrid.NodeLeft    += ScenarioGrid_NodeLeft;

            // Load UI (ScenarioStateUI partial class)
            LoadUI();
        }
 public static List<Cell> PathToTarget(this GameMap gm, Cell origin, Cell goal)
 {
     var pathFinder = new AStarPathfinder();
     pathFinder.FindPath(origin, goal, gm.CellGameMap, false);
     return pathFinder.CellsFromPath();
 }
 private void OnEnable()
 {
     targetPathing = (AStarPathfinder)target;
 }