Ejemplo n.º 1
0
    public virtual SquareGridArea GetMoveableArea(SquareGrid grid)
    {
        HashSet <SquareGridSquare> in_range;
        Queue <SquareGridSquare>   q;
        int depth;
        int max_depth;
        int squares_in_next_depth;
        int squares_in_curr_depth;

        /* meh improve later */
        in_range = new HashSet <SquareGridSquare>();
        foreach (var s in grid.GetAdjacentRadius(this.square, this.move_range))
        {
            s.TmpVertex.Reset();
        }

        /* TODO: try to abstract the bfs into GraphableMixin */


        depth                 = 0;
        max_depth             = this.move_range;
        squares_in_curr_depth = 1;
        squares_in_next_depth = 0;

        this.square.TmpVertex.Reset();
        this.square.TmpVertex.score = 1;         // discovered
        q = new Queue <SquareGridSquare>();
        q.Enqueue(this.square);

        while (q.Count > 0)
        {
            var s = q.Dequeue();

            foreach (var adj in s.Vertex.Adjacent)
            {
                /* see ResetForSearch for what the undiscovered score is */
                if ((adj.TmpVertex.score != 1) &&
                    this.CanMoveAdjacent(s, adj))
                {
                    s.TmpVertex.AddAdjacent(adj.TmpVertex);
                    adj.TmpVertex.score = 1;                     // discovered

                    q.Enqueue(adj);
                    in_range.Add(adj);
                    squares_in_next_depth += 1;
                }
            }

            squares_in_curr_depth -= 1;
            if (squares_in_curr_depth == 0)
            {
                squares_in_curr_depth = squares_in_next_depth;
                squares_in_next_depth = 0;
                depth += 1;
                if (depth >= max_depth)
                {
                    break;
                }
            }
        }

        return(new SquareGridArea(
                   in_range, this.square));
    }