Beispiel #1
0
        /// <summary>
        /// The meat of the JPS Search
        /// </summary>
        /// <param name="map">The HexMap</param>
        /// <returns>A list of jump points to get from start to end.</returns>
        private static List <Hexagon <T> > JPSSearch(HexMap <T> map)
        {
            var directions = (HexDirection[])Enum.GetValues(typeof(HexDirection));

            var jumpPoints = new List <Hexagon <T> >();

            // HexagonsToSearch should contain only the start hexagon right now.
            while (HexagonsToSearch.Count > 0)
            {
                // get next item
                var hexToExamine = GetMinFromSearchList();

                // do stuff with hexToExamine
                // iterate through the neighbors of hexToExamine's neighbors
                foreach (var direction in directions)
                {
                    var dist = hexToExamine.DistanceFromStart;

                    var neighbor = hexToExamine.Neighbors[direction];
                    // go in the direction of this neighbor until
                    // obstacle, undefined hex (out of map/hole), or forced neighbor
                    while (neighbor != null && FreeHex(neighbor) && !HasForcedNeighbor(neighbor, direction))
                    {
                        // everytime we hop away from hexToExamine, increment the distance by 1
                        // Since the algorithm is essentially a BFS with some other stuff,
                        // even if we take 3 right turns (for example) the distance metric should still be okay.
                        dist++;
                        neighbor = neighbor.Neighbors[direction];
                    }
                    // now neighbor is either invalid/obstacle, in which case we can just stop
                    if (neighbor == null || !FreeHex(neighbor))
                    {
                        continue;
                    }
                    else
                    {
                        // we must have a forced neighbor
                        // we don't actually need to know where the forced neighbor is
                        // just that the current neighbor variable is a jump point we need to consider
                        neighbor.DistanceFromStart = dist;
                        AddHexToSearchList(neighbor);
                    }
                }
            }

            // now that End is found, go backwards by following Hexagon.prevJumpPoint to Start
            var goBack = map.End;

            while (goBack != map.Start)
            {
                jumpPoints.Add(goBack);
                goBack = goBack.prevJumpPoint;
            }
            jumpPoints.Add(goBack); // goBack is map.Start now...
            return(jumpPoints);
        }
Beispiel #2
0
        public Hexagon(int cubeX, int cubeY, int cubeZ, T obj) : this(obj)
        {
            CubeX = cubeX;
            CubeY = cubeY;
            CubeZ = cubeZ;

            var axial = HexMap <T> .CubicToAxial(new Tuple <int, int, int>(cubeX, cubeY, cubeZ));

            AxialX = axial.First;
            AxialZ = axial.Second;
        }
Beispiel #3
0
        public Hexagon(int axialX, int axialZ, T obj) : this(obj)
        {
            AxialX = axialX;
            AxialZ = axialZ;

            var cubic = HexMap <T> .AxialToCubic(new Tuple <int, int>(axialX, axialZ));

            CubeX = cubic.First;
            CubeY = cubic.Second;
            CubeZ = cubic.Third;
        }
Beispiel #4
0
        /// <summary>
        /// Do a pathfinding search from Start to End with respect to _hexes, avoiding _obstacles
        /// This algorithm is WIP. It should be optimized A*, but unproven.
        /// </summary>
        /// <param name="map">The HexWorld map that contains the start/end, tiles, and obstacles</param>
        /// <returns>A list of hexagons representing a path from start to end</returns>
        public static List <Hexagon <T> > JPSPath(HexMap <T> map)
        {
            // Clear hexagon to consider list before adding start.
            while (HexagonsToSearch.Count > 0)
            {
                IPriorityQueueHandle <Hexagon <T> > tmp;
                HexagonsToSearch.DeleteMin(out tmp);
            }
            HexRefList.Clear();


            // Add the start to the search list to start it off
            map.Start.DistanceFromStart = 0;
            AddHexToSearchList(map.Start);

            var jumpPoints = JPSSearch(map);

            //return JumpPointsToPath(jumpPoints);
            return(jumpPoints);
        }