Пример #1
0
        public List <T> get_route(T loc, T target_loc, int mov)
        {
            bool restrict_to_map        = Map.RestrictToMap(loc, target_loc);
            bool use_euclidean_distance = false; //@Debug: not really beneficial even with -1 mov

            //Prepare pathfinding variables
            OpenList <T>        open_list   = new OpenList <T>();
            ClosedListRoute <T> closed_list = new ClosedListRoute <T>();

            // Start pathfinding
            bool route_found = false;

            open_list.add_initial_item(
                loc, Map.Distance(loc, target_loc, use_euclidean_distance));
            int last_added = -1;

            for (; ;)
            {
                if (open_list.size <= 0)
                {
                    break;
                }

                OpenItem <T> lowest_f_item = open_list.remove_open_item();
                last_added = closed_list.add_item(lowest_f_item);
                T current_loc = lowest_f_item.Loc;

                if (current_loc.Equals(target_loc))
                {
                    route_found = true;
                    break;
                }
                else
                {
                    IEnumerable <T> check_locs = Map.AdjacentLocations(current_loc);
                    foreach (T test_loc in check_locs)
                    {
                        // If the location is already on the closed list
                        if (closed_list.already_added(test_loc))
                        {
                            continue;
                        }
                        if (Map.InvalidLocation(test_loc, target_loc, restrict_to_map))
                        {
                            continue;
                        }
                        check_tile(test_loc, last_added, mov, target_loc,
                                   open_list, closed_list, use_euclidean_distance: use_euclidean_distance);
                    }
                }
            }
            RouteDistance = 0;
            if (route_found)
            {
                RouteDistance = closed_list.get_g(last_added) / COST_FACTOR;
                return(closed_list.get_route(last_added));
            }
            return(null);
        }
Пример #2
0
        private void swap(int index1, int index2)
        {
            OpenItem <T> temp = Items[index2];

            Items[index2] = Items[index1];
            Items[index1] = temp;

            update_location_index(index1);
            update_location_index(index2);
        }
Пример #3
0
        private ClosedListRoute <T> GetRange(T loc, T target_loc, int mov, bool dijkstras)
        {
            //Prepare pathfinding variables
            OpenList <T>        open_list   = new OpenList <T>();
            ClosedListRoute <T> closed_list = new ClosedListRoute <T>();

            // Start pathfinding
            open_list.add_initial_item(
                loc, Map.Distance(loc, target_loc));
            int last_added = -1;

            for (;;)
            {
                if (open_list.size <= 0)
                {
                    break;
                }

                OpenItem <T> lowest_f_item = open_list.remove_open_item();
                last_added = closed_list.add_item(lowest_f_item);
                T current_loc = lowest_f_item.Loc;

                bool reverse = (rand.Next(2) == 0);
                reverse = false;
                IEnumerable <T> check_locs = Map.AdjacentLocations(current_loc);
                if (reverse)
                {
                    check_locs = check_locs.Reverse();
                }
                foreach (T test_loc in check_locs)
                {
                    // If the location is already on the closed list
                    if (closed_list.already_added(test_loc))
                    {
                        continue;
                    }
                    if (Map.InvalidLocation(test_loc, target_loc, true))
                    {
                        continue;
                    }
                    check_tile(test_loc, last_added, mov, target_loc,
                               open_list, closed_list,
                               dijkstras: dijkstras);
                }
            }

            return(closed_list);
        }
Пример #4
0
        public OpenItem <T> remove_open_item()
        {
            // Get Item with the lowest F cost
            int          remove_id     = lowest_f_id();
            OpenItem <T> lowest_f_item = Items[remove_id];

            Lowest_F_Id = -1;

            // Reduce size of list
            LocationIndices.Remove(lowest_f_item.Loc);
            Size--;

            // If the removed item index is within the valid size range
            if (remove_id < this.size)
            {
                // Move last item into the removed item's position
                Items[remove_id] = Items[this.size];
                update_location_index(remove_id);
                // Percolate down the item that was moved up
                heapify(remove_id + 1);
            }

            return(lowest_f_item);
        }
Пример #5
0
        public List <T> get_reverse_route(T loc, T target_loc, int mov)
        {
            bool restrict_to_map = Map.RestrictToMap(loc, target_loc);

            //Prepare pathfinding variables
            OpenList <T>        open_list   = new OpenList <T>();
            ClosedListRoute <T> closed_list = new ClosedListRoute <T>();

            // Start pathfinding
            bool route_found = false;

            open_list.add_initial_item(loc, Map.TileCost(loc, target_loc), Map.Distance(loc, target_loc));
            int last_added = -1;

            for (; ;)
            {
                if (open_list.size <= 0)
                {
                    break;
                }

                if (ReverseRouteWiggleChance != null)
                {
                    open_list.rng_lowest_f_id(ReverseRouteWiggleChance);
                }
                OpenItem <T> lowest_f_item = open_list.remove_open_item();
                last_added = closed_list.add_item(lowest_f_item);
                T current_loc = lowest_f_item.Loc;

#if DEBUG
                if (current_loc.Equals(target_loc))
                {
                    throw new IndexOutOfRangeException("Target location somehow got added to closed list");
                }
#endif
                if (Map.IsAdjacent(current_loc, target_loc))
                {
                    route_found = true;
                    break;
                }
                else
                {
                    IEnumerable <T> check_locs = Map.AdjacentLocations(current_loc);
                    foreach (T test_loc in check_locs)
                    {
                        // If the location is already on the closed list
                        if (closed_list.already_added(test_loc))
                        {
                            continue;
                        }
                        if (Map.InvalidLocation(test_loc, target_loc, restrict_to_map))
                        {
                            continue;
                        }
                        check_tile(test_loc, last_added, mov, target_loc,
                                   open_list, closed_list);
                    }
                }
            }
            RouteDistance = 0;
            if (route_found)
            {
                RouteDistance = closed_list.get_g(last_added) / COST_FACTOR;
                return(closed_list.get_reverse_route(last_added, target_loc));
            }
            return(null);
        }
Пример #6
0
        public Maybe <int> get_distance(T loc, T target_loc, int mov)
        {
            bool restrict_to_map = Map.RestrictToMap(loc, target_loc, false);
            //Prepare pathfinding variables
            OpenList <T>        open_list   = new OpenList <T>();
            ClosedListRoute <T> closed_list = new ClosedListRoute <T>();

            // Start pathfinding
            bool route_found = false;

            open_list.add_initial_item(
                loc, Map.Distance(loc, target_loc, false));
            int last_added = -1;

            for (; ;)
            {
                if (open_list.size <= 0)
                {
                    break;
                }
                // Most time is spent adding and removing items from the open list,
                // and checking if a tile is Passable //Profiler
                OpenItem <T> lowest_f_item = open_list.remove_open_item();
                last_added = closed_list.add_item(lowest_f_item);
                T current_loc = lowest_f_item.Loc;

                if (current_loc.Equals(target_loc))
                {
                    route_found = true;
                    break;
                }
                else
                {
                    bool reverse = (rand.Next(2) == 0);
                    reverse = false;
                    IEnumerable <T> check_locs = Map.AdjacentLocations(current_loc);
                    if (reverse)
                    {
                        check_locs = check_locs.Reverse();
                    }
                    foreach (T test_loc in check_locs)
                    {
                        // If the location is already on the closed list
                        if (closed_list.already_added(test_loc))
                        {
                            continue;
                        }
                        if (Map.InvalidLocation(test_loc, restrict_to_map))
                        {
                            continue;
                        }
                        check_tile(test_loc, last_added, mov, target_loc,
                                   open_list, closed_list);
                    }
                }
            }
            RouteDistance = 0;
            if (route_found)
            {
                return(closed_list.get_g(last_added) / COST_FACTOR);
            }
            return(new Maybe <int>());
        }
Пример #7
0
 public int add_item(OpenItem <T> openItem)
 {
     return(add_item(openItem.Loc, openItem.Parent,
                     openItem.Fcost, openItem.Gcost, openItem.Accessible));
 }