Esempio n. 1
0
    public void Init(int w, int h, float _tilesize)
    {
        // 设置耗费值和网格方位
        dirX = new int[8] {
            -1, 0, 1, 1, 1, 0, -1, -1
        };
        dirY = new int[8] {
            -1, -1, -1, 0, 1, 1, 1, 0
        };
        Cost = new int[8] {
            14, 10, 14, 10, 14, 10, 14, 10
        };

        width    = w;
        height   = h;
        tilesize = _tilesize;

        tiles = new AStarTile[w, h];
        for (int y = 0; y < height; ++y)
        {
            for (int x = 0; x < width; ++x)
            {
                tiles[x, y] = new AStarTile(x, y);
            }
        }
    }
        private void DrawTile(object sender, NodeAddedToCollectionEventArgs args)
        {
            mainDispatcher.Invoke(() =>
            {
                Node node      = args.node;
                AStarTile tile = AStarValues.AStarTiles[node.RowIndex, node.ColumnIndex];
                if (tile == null)
                {
                    tile = new AStarTile(node.RowIndex, node.ColumnIndex);
                }

                Tile tileType = tile.TileType;
                if (tileType == Tile.Empty || tileType == Tile.EmptyClosed || tileType == Tile.EmptyOpen)
                {
                    Tile newType = Tile.Empty;

                    if (sender is OpenSet)
                    {
                        newType = Tile.EmptyOpen;
                    }
                    else if (sender is ClosedSet)
                    {
                        newType = Tile.EmptyClosed;
                    }

                    tile.TileType = newType;

                    AStarValues.SetAStarTile(tile);
                }
            }, DispatcherPriority.Normal);
        }
Esempio n. 3
0
        private void RemoveTile(object sender, MouseEventArgs e)
        {
            if (AStarValues.AStarState == State.HasNotStarted)
            {
                Point  mousePosition = e.GetPosition(canvas);
                double MousePosX     = mousePosition.X;
                double MousePosY     = mousePosition.Y;

                int RowIndex    = GetRowIndex(MousePosY);
                int ColumnIndex = GetColumnIndex(MousePosX);

                AStarTile oldTile = AStarValues.AStarTiles[RowIndex, ColumnIndex];

                if (oldTile == null)
                {
                    return;
                }

                if (oldTile.TileType == Tile.Start)
                {
                    AStarValues.StartTile = null;
                }
                else if (oldTile.TileType == Tile.Goal)
                {
                    AStarValues.GoalTile = null;
                }

                var emptyTile = new AStarTile(RowIndex, ColumnIndex, Tile.Empty);
                AStarValues.SetAStarTile(emptyTile);
            }
        }
Esempio n. 4
0
    public bool PathFind(AStarTile start, AStarTile end, List <AStarTile> except)
    {
        Reset(false);

        Path.Clear();
        PathFindSuccess   = false;
        PathFindCompleted = false;
        PathCount         = 0;
        CheckStep         = 0;
        tileStart         = start;
        tileEnd           = end;

        tileInCheck      = tileStart;
        tileInCheck.open = 0;
        tileInCheck.g    = 0;

        if (except == null)
        {
            tileExcept = new List <AStarTile>();
        }
        else
        {
            tileExcept = except;
        }

        PathCount++;

        while (!PathFindCompleted)
        {
            PathFindSub();
        }

        return(PathFindSuccess);
    }
Esempio n. 5
0
    private void AddTiletoOpenList(int x, int y, AStarTile prevTile)
    {
        AStarTile addTile = map[x, y];

        if (addTile.tile.value != TileValue.Floor || closedList.Contains(addTile))
        {
            return;
        }

        if (openList.Contains(addTile))
        {
            if (addTile.startScore > prevTile.startScore + 1)
            {
                addTile.startScore = prevTile.startScore + 1;
                addTile.parent     = prevTile;
            }
        }
        else
        {
            openList.Add(addTile);
            addTile.startScore  = prevTile.startScore + 1;
            addTile.parent      = prevTile;
            addTile.finishScore = GetDistance(addTile.tile, finish.tile);
        }
    }
Esempio n. 6
0
 // Constructor
 public AStarTile(Tile linkedTile)
 {
     displayTile   = linkedTile;
     parent        = null;
     distFromStart = 0;
     distFromEnd   = 0;
 }
Esempio n. 7
0
    private void AddNeighboursToOpenList(AStarTile previousTile)
    {
        //left
        if (previousTile.tile.x > 1)
        {
            AddTiletoOpenList(previousTile.tile.x - 1, previousTile.tile.y, previousTile);
        }

        //right
        if (previousTile.tile.x < map.GetLength(0) - 2)
        {
            AddTiletoOpenList(previousTile.tile.x + 1, previousTile.tile.y, previousTile);
        }

        //down
        if (previousTile.tile.y > 1)
        {
            AddTiletoOpenList(previousTile.tile.x, previousTile.tile.y - 1, previousTile);
        }

        //top
        if (previousTile.tile.y < map.GetLength(1) - 2)
        {
            AddTiletoOpenList(previousTile.tile.x, previousTile.tile.y + 1, previousTile);
        }
    }
Esempio n. 8
0
    public int HeuristicCalculation(AStarTile current, AStarTile goal)
    {
        int manhattanValue = 100 - Mathf.RoundToInt(sliderBar.value);
        int euclideanValue = Mathf.RoundToInt(sliderBar.value);

        return((ManHattanDistance(current, goal) * manhattanValue) + (EuclidianDistance(current, goal) * euclideanValue));
    }
Esempio n. 9
0
 public static void PerformSearch(AStarTile startingPoint, AStarTile endingPoint)
 {
     //Assemble the tile representation of the wordl
     ms_instance.StartCoroutine(ms_instance.PerformAStarSearchCoroutine(
                                    startingPoint,
                                    endingPoint));
 }
Esempio n. 10
0
    //Try to find the nearest path to the fruit, takes in position of the snakehead
    public List <Vector2Int> CalculatePath(Vector2Int position)
    {
        //Clear previous calculation
        tileList.Clear();
        path.Clear();
        currentPos = position;
        sourcePos  = position;
        //Marks the snakehead as origin
        AddInitialTile(position);
        //Finds position of fruit
        Vector2Int _target = FindFruit();

        //Calculate costs of neighboring tiles
        CheckNeighbors(position, _target);

        //Find the path
        while (currentPos != _target)
        {
            //Checks if there is any tile left to calculate, returns the lowest cost tile.
            if (FindNextTile(out AStarTile _tile))
            {
                CheckNeighbors(_tile.position, _target);
            }
            //If path to fruit cannot be found, reset the list and try to find a path to the tail
            else
            {
                tileList.Clear();
                currentPos = position;
                AddInitialTile(position);
                CheckNeighbors(position, data.snakeList.Last.position);
                while (currentPos != data.snakeList.Last.position)
                {
                    if (FindNextTile(out _tile))
                    {
                        CheckNeighbors(_tile.position, data.snakeList.Last.position);
                    }
                    else
                    {
                        break;
                    }
                }
                break;
            }
        }

        //Takes the last tile and tracks back to the snake
        AStarTile _currentTile = tileList[currentPos];

        while (_currentTile.previousTile != null)
        {
            path.Add(_currentTile.position);
            _currentTile = _currentTile.previousTile;
        }
        path.Add(position);
        path.Reverse();

        //Return the per-tile path that the snake is to take
        return(path);
    }
Esempio n. 11
0
    private void AddInitialTile(Vector2Int position)
    {
        AStarTile _tile = new AStarTile(position);

        tileList.Add(position, _tile);
        _tile.explored = true;
        currentPos     = position;
    }
Esempio n. 12
0
        private void SetGoalTile(int RowIndex, int ColumnIndex)
        {
            var goalTile = new AStarTile(RowIndex, ColumnIndex, Tile.Goal);

            AStarValues.SetAStarTile(goalTile);
            AStarValues.GoalTile = goalTile;

            SetGoalTileValueChanged(this, EventArgs.Empty);
        }
Esempio n. 13
0
        private void SetStartTile(int RowIndex, int ColumnIndex)
        {
            var startTile = new AStarTile(RowIndex, ColumnIndex, Tile.Start);

            AStarValues.SetAStarTile(startTile);
            AStarValues.StartTile = startTile;

            SetStartTileValueChanged(this, EventArgs.Empty);
        }
Esempio n. 14
0
    // Draw a line to the board.
    private void DrawPathLine(AStarTile endTile)
    {
        AStarTile curTile = endTile;

        while (curTile != null)
        {
            level[curTile.pos.x, curTile.pos.y].type = LevelTileType.Corridor;
            curTile = curTile.prev;
        }
    }
Esempio n. 15
0
        private Rectangle GetRectangle(Canvas DrawingCanvas, AStarTile tile)
        {
            var rect = new Rectangle();

            int numrows    = AStarValues.NumGridRows;
            int numcolumns = AStarValues.NumGridColumns;

            double canvasheight = DrawingCanvas.ActualHeight;
            double canvaswidth  = DrawingCanvas.ActualWidth;

            double rowspacing    = canvasheight / numrows;
            double columnspacing = canvaswidth / numcolumns;

            rect.Height = rowspacing;
            rect.Width  = columnspacing;

            rect.Stroke = new SolidColorBrush(Colors.Transparent);
            rect.SnapsToDevicePixels = true;


            switch (tile.TileType)
            {
            case Tile.Empty:
                rect.Fill = new SolidColorBrush(Colors.Transparent);
                break;

            case Tile.EmptyClosed:
                rect.Fill = RenderColors.ClosedTile;
                break;

            case Tile.EmptyOpen:
                rect.Fill = RenderColors.OpenTile;
                break;

            case Tile.Start:
                rect.Fill = RenderColors.StartTile;
                break;

            case Tile.Wall:
                rect.Fill = RenderColors.WallTile;
                break;

            case Tile.Goal:
                rect.Fill = RenderColors.GoalTile;
                break;
            }

            double topdistance  = tile.RowIndex * rowspacing;
            double leftdistance = tile.ColumnIndex * columnspacing;

            Canvas.SetLeft(rect, leftdistance);
            Canvas.SetTop(rect, topdistance);

            return(rect);
        }
Esempio n. 16
0
        public Node(AStarTile tile, Node parent = null)
        {
            Id = Count;
            Count++;

            RowIndex    = tile.RowIndex;
            ColumnIndex = tile.ColumnIndex;
            Parent      = parent;

            IsWalkable = !(tile.TileType == Tile.Wall);
        }
Esempio n. 17
0
    private void ReconstructPath()
    {
        AStarTile currentTile = finish;

        path.Add(finish.tile);
        while (currentTile.parent != null)
        {
            path.Add(currentTile.parent.tile);
            currentTile = currentTile.parent;
        }
        path.Reverse();
    }
Esempio n. 18
0
        private void Render(object sender, int RowIndex, int ColumnIndex)
        {
            TileRenderObject oldTile = Tiles[RowIndex, ColumnIndex];

            AStarTile        newAStarTile = AStarValues.AStarTiles[RowIndex, ColumnIndex];
            TileRenderObject newTile      = new TileRenderObject(DrawingCanvas, newAStarTile);

            Tiles[RowIndex, ColumnIndex] = newTile;

            DrawingCanvas.Children.Add(newTile.Shape);
            DrawingCanvas.Children.Remove(oldTile.Shape);
        }
Esempio n. 19
0
    public void SetTileMode(int x, int y, int type)
    {
        AStarTile tile = tiles[x, y];

        //if((type == AStarTileType.Start) && (tileStart != null)) { tileStart.SetType(0); tileStart = null; }
        //if((type == AStarTileType.End  ) && (tileEnd   != null)) { tileEnd.SetType(0);   tileEnd = null; }

        tile.SetType(type);

        //if(type == AStarTileType.Start)	tileStart = tile;
        //if(type == AStarTileType.End  )   tileEnd = tile;
    }
Esempio n. 20
0
    public void Reset(bool SetBlank)
    {
        parent = null;
        open   = -1;
        g      = 0;
        h      = -1;
        f      = 0;

        if (SetBlank)
        {
            SetType(0);
        }
    }
Esempio n. 21
0
    // Overriden so the base class can be called.
    protected override void Start()
    {
        base.Start();

        aStarTiles = new AStarTile[sizeX, sizeY];

        for (int x = 0; x < sizeX; x++)
        {
            for (int y = 0; y < sizeY; y++)
            {
                aStarTiles[x, y] = new AStarTile(tileGrid[x, y]);
            }
        }
    }
Esempio n. 22
0
    public override bool CreatePath(Tile[,] map, Tile start, Tile finish)
    {
        openList   = new List <AStarTile>();
        closedList = new List <AStarTile>();
        path       = new List <Tile>();

        ConvertMap(map);
        this.start = ConvertTile(start);
        this.map[start.x, start.y] = this.start;
        this.finish = ConvertTile(finish);
        this.map[finish.x, finish.y] = this.finish;

        return(AStarPath());
    }
Esempio n. 23
0
    public void PathFindSub()
    {
        CheckStep++;
        Check(tileInCheck);

        AStarTile tileMinF = null;

        for (int y = 0; y < height; ++y)
        {
            for (int x = 0; x < width; ++x)
            {
                if (tiles[x, y].open != 1)
                {
                    continue;
                }

                if ((tileMinF == null) || (tiles[x, y].f < tileMinF.f))
                {
                    tileMinF = tiles[x, y];
                }
            }
        }

        if (tileMinF == null)
        {
            PathFindCompleted = true;
            return;
        }

        tileInCheck      = tileMinF;
        tileInCheck.open = 0;
        //Debug.Log ("A Star "+PathCount.ToString ()+" - "+tileInCheck.x.ToString()+","+tileInCheck.y.ToString()+",g:"+tileInCheck.g.ToString()+",h:"+tileInCheck.h.ToString()+",f:"+tileInCheck.f.ToString());
        PathCount++;

        if (tileInCheck == tileEnd)
        {
            PathFindCompleted = true;
            PathFindSuccess   = true;

            AStarTile tileResult = tileEnd;
            while (tileResult != null)
            {
                Path.Insert(0, GetTilePos(tileResult.x, tileResult.y));
                tileResult = tileResult.parent;
            }
        }
    }
Esempio n. 24
0
        /// <summary>
        /// Assembles a path from the data stored in the AStarTileGrid, tracing the tiles using each tile's parent location.
        /// </summary>
        /// <param name="aStarTileGrid"></param>
        /// <param name="endTile"></param>
        /// <param name="startingLocation"></param>
        /// <returns></returns>
        private AStarPath BuildPath(ref AStarTileGrid aStarTileGrid, AStarTile endTile, Vector2Int startingLocation)
        {
            AStarTile priorTile = endTile;
            AStarPath path      = new AStarPath();

            //Trace through each parent, as the path is effectively stored as a linked list, where the endTile is the start of the list.
            path.AddTile(tiles[endTile.location]);
            while (priorTile.location != startingLocation)
            {
                path.AddTile(tiles[priorTile.location]);
                priorTile = aStarTileGrid[priorTile.parentLocation];
            }

            //The starting location is not added in the above loop, so we manually add it after
            path.AddTile(tiles[startingLocation]);
            return(path);
        }
Esempio n. 25
0
        private void ClearTiles_Pressed(object sender, EventArgs args)
        {
            int numRows    = AStarValues.NumGridRows;
            int numColumns = AStarValues.NumGridColumns;

            AStarValues.StartTile = null;
            AStarValues.GoalTile  = null;

            for (int i = 0; i < numRows; i++)
            {
                for (int j = 0; j < numColumns; j++)
                {
                    var newTile = new AStarTile(i, j, Tile.Empty);
                    AStarValues.SetAStarTile(newTile);
                }
            }
        }
Esempio n. 26
0
    //Calculates the tiles G-cost
    public int DistanceFromSource()
    {
        int       _count       = 0;
        AStarTile _currentTile = this;

        while (_currentTile.previousTile != null)
        {
            _count++;
            _currentTile = _currentTile.previousTile;
            if (_currentTile.previousTile == this)
            {
                throw new System.Exception("Stuck in loop");
            }
        }

        return(_count);
    }
Esempio n. 27
0
        public TileRenderer(Canvas canvas)
        {
            this.DrawingCanvas = canvas;

            int numRows    = AStarValues.NumGridRows;
            int numColumns = AStarValues.NumGridColumns;

            this.Tiles = new TileRenderObject[numRows, numColumns];
            for (int i = 0; i < numRows; i++)
            {
                for (int j = 0; j < numColumns; j++)
                {
                    var tile = new AStarTile(i, j);
                    Tiles[i, j] = new TileRenderObject(DrawingCanvas, tile);
                }
            }
        }
Esempio n. 28
0
    public void Reset(bool SetBlank)
    {
        PathFindCompleted = false;
        if (SetBlank)
        {
            tileStart   = null;
            tileEnd     = null;
            tileInCheck = null;
        }

        for (int y = 0; y < height; ++y)
        {
            for (int x = 0; x < width; ++x)
            {
                tiles[x, y].Reset(SetBlank);
            }
        }
    }
        private void SetupMap()
        {
            this.tiles = AStarValues.AStarTiles;
            int numRows    = tiles.GetLength(0);
            int numColumns = tiles.GetLength(1);

            this.map = new Node[numRows, numColumns];
            var nodes = new Node[numRows, numColumns];

            for (int row = 0; row < numRows; row++)
            {
                for (int col = 0; col < numColumns; col++)
                {
                    AStarTile tile;
                    if (tiles[row, col] == null)
                    {
                        tile = new AStarTile(row, col);
                    }
                    else
                    {
                        tile = tiles[row, col];
                    }

                    Node node = new Node(tile);

                    Tile type = tile.TileType;
                    switch (type)
                    {
                    case Tile.Start:
                        StartNode = node;
                        break;

                    case Tile.Goal:
                        GoalNode = node;
                        break;

                    default:
                        break;
                    }

                    map[row, col] = node;
                }
            }
        }
Esempio n. 30
0
    private bool AStarPath()
    {
        start.startScore  = 0;
        start.finishScore = GetDistance(start.tile, finish.tile);
        start.parent      = null;
        closedList.Add(start);
        AddNeighboursToOpenList(start);
        while (openList.Count > 0)
        {
            AStarTile currentTile = GetLowestScoreTile();
            if (currentTile.tile == finish.tile)
            {
                ReconstructPath();
                return(true);
            }

            openList.Remove(currentTile);
            closedList.Add(currentTile);

            AddNeighboursToOpenList(currentTile);
        }
        return(false);
    }