示例#1
0
        /// <summary>
        /// Gets the accessible adjacent locations of a specified location.
        /// A location is accessible if it's top, bottom, left or right of the location within the grid's ranges and is not a treasure.
        /// </summary>
        /// <returns>The accessible adjacent locations.</returns>
        /// <param name="gameGrid">Game grid.</param>
        /// <param name="location">Location.</param>
        /// <param name="ignoreLocation">Ignore this location no matter what type of treasure it has.</param>
        /// <param name="currentActiveTreasure">The current active treasure is never considered a wall.</param> 
        public static List<GridLocation> GetAccessibleAdjacentLocations(GameFieldGrid gameGrid, GridLocation location, TreasureType currentActiveTreasure, GridLocation ignoreLocation = null)
        {
            var checkLocations = new List<GridLocation> ();

            // Check the item to the top.
            if(location.Row > 0)
            {
                var targetLocation = new GridLocation (location.Row - 1, location.Column);
                bool ignore = ignoreLocation != null && targetLocation.Equals (ignoreLocation);
                bool isActiveTreasure = gameGrid.GetItem (targetLocation).Treasure == currentActiveTreasure;

                if (isActiveTreasure || ignore || IsPassableTreasureType (targetLocation, gameGrid))
                {
                    checkLocations.Add (targetLocation);
                }
            }

            // Check the item below.

            if(location.Row < gameGrid.Rows - 1)
            {
                var targetLocation = new GridLocation (location.Row + 1, location.Column);
                var ignore = ignoreLocation != null && targetLocation.Equals (ignoreLocation);
                bool isActiveTreasure = gameGrid.GetItem (targetLocation).Treasure == currentActiveTreasure;

                if (isActiveTreasure || ignore || IsPassableTreasureType (targetLocation, gameGrid))
                {
                    checkLocations.Add (targetLocation);
                }
            }

            // Check the item to the left.
            if(location.Column > 0)
            {
                var targetLocation = new GridLocation (location.Row, location.Column - 1);
                var ignore = ignoreLocation != null && targetLocation.Equals (ignoreLocation);
                bool isActiveTreasure = gameGrid.GetItem (targetLocation).Treasure == currentActiveTreasure;

                if (isActiveTreasure || ignore || IsPassableTreasureType (targetLocation, gameGrid))
                {
                    checkLocations.Add (targetLocation);
                }
            }

            // Check the item to the right.
            if(location.Column < gameGrid.Columns - 1)
            {
                var targetLocation = new GridLocation (location.Row, location.Column + 1);
                var ignore = ignoreLocation != null && targetLocation.Equals (ignoreLocation);
                bool isActiveTreasure = gameGrid.GetItem (targetLocation).Treasure == currentActiveTreasure;

                if (isActiveTreasure || ignore || IsPassableTreasureType (targetLocation, gameGrid))
                {
                    checkLocations.Add (targetLocation);
                }
            }

            return checkLocations;
        }
示例#2
0
    private List <GridLocation> LazyRun(Bitmap bitmap, GridLocation start, GridLocation goal)
    {
        //Debug.LogWarning("TS: setup dictionaries");
        Dictionary <GridLocation, GridLocation> cameFrom  = new Dictionary <GridLocation, GridLocation>();
        Dictionary <GridLocation, double>       costSoFar = new Dictionary <GridLocation, double>();

        //Debug.LogWarning("TS: setup interval heap");
        var frontier = new C5.IntervalHeap <GridLocation>();

        start.priority = 0;
        frontier.Add(start);
        cameFrom[start]  = null;
        costSoFar[start] = 0;

        //Debug.LogWarning("TS: while loop BEGIN");
        float exitLoopTime = Time.time + 5f;

        while (!frontier.IsEmpty)
        {
            if (Time.time > exitLoopTime)
            {
                Debug.LogWarning("TS: theta star timeout");
                return(null);
            }

            //Debug.LogWarning("TS: while loop entered");
            GridLocation current = frontier.DeleteMin();
            //Debug.LogWarning("TS: while loop SetVertex");
            SetVertex(bitmap, cameFrom, costSoFar, current);
            if (current.Equals(goal))
            {
                //Debug.LogWarning("TS: current == goal");
                return(GridSearch.RebuildPath(goal, cameFrom));
            }

            closedSet.Add(current);
            foreach (GridLocation next in bitmap.Neighbours(current))
            {
                double computedCost = lazyComputeCost(cameFrom, costSoFar, current, next);
                if (!costSoFar.ContainsKey(next) || computedCost < costSoFar[next])
                {
                    cameFrom[next]  = cameFrom[current];
                    costSoFar[next] = computedCost;
                    double p = computedCost + next.distanceTo(goal);
                    next.priority = p;
                    frontier.Add(next);
                }
            }
        }

        Debug.LogWarning("TS: returning null");
        return(null);
    }
    public static bool CanPlaceAt(GridLocation loc)
    {
        if (loc == null)
        {
            return(false);
        }

        // -- name data and grab bc things are getting complicated
        bool objectwalkable            = instance.currentlevelqueue.GetNextItemCost().walkable;
        bool gridwalkable              = instance.grid[loc.x, loc.y].walkable;
        bool breakable                 = instance.currentlevelqueue.GetNextItemCost().breakable;
        bool squarealreadyhasbreakable = instance.grid[loc.x, loc.y].hasbreakableitem;
        bool occupied = instance.grid[loc.x, loc.y].occupied;

        bool squareclear = !occupied || (gridwalkable && !objectwalkable) || breakable;

        // -- make sure the player isnt in or traveling to this location
        bool playerclear = !loc.Equals(instance.player.GetCurrentGridLocation()) &&
                           !loc.Equals(instance.player.GetNextGridLocation());

        return(squareclear && playerclear);
    }
        /// <summary>
        /// GetWordPlacement
        /// Attempts to find a place in the grid for the given word
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="word"></param>
        /// <returns></returns>
        public HiddenWord GetWordPlacement(char[,] grid, string word)
        {
            var        startLocationFound  = false;
            var        entireGridSearched  = false;
            var        initialGridLocation = new GridLocation(_randomNumberService, grid);
            var        gridLocation        = new GridLocation(initialGridLocation);
            HiddenWord hiddenWord          = null;

            // Randomize direction list
            Random rnd        = new Random();
            var    directions = _directions.OrderBy(x => rnd.Next()).ToArray();

            // move right and down, checking if word can fit into any direction. if so add it
            do
            {
                // check word can be placed in any of the directions
                foreach (var direction in directions)
                {
                    hiddenWord = new HiddenWord()
                    {
                        Direction = direction,
                        Start     = gridLocation,
                        Word      = word
                    };

                    if (startLocationFound = PlacementValidator.IsWordPlacementValid(grid, hiddenWord))
                    {
                        break;
                    }
                }

                if (!startLocationFound)
                {
                    // Move to next position
                    gridLocation       = grid.GetNextGridLocation(gridLocation);
                    entireGridSearched = gridLocation.Equals(initialGridLocation);
                }
            }while (!startLocationFound && !entireGridSearched);

            return(startLocationFound ? hiddenWord : null);
        }