Exemple #1
0
        public void repoint(int index, int parent, int f, int g)
        {
            if (g < Items[index].Gcost)
            {
                Items[index].Parent = parent;
                Items[index].Fcost  = f;
                Items[index].Gcost  = g;

                int i = index + 1;
                while (i != 1)
                {
                    if (Items[i - 1].Fcost <= Items[i / 2 - 1].Fcost)
                    {
                        OpenItem temp = Items[i / 2 - 1];
                        Items[i / 2 - 1] = Items[i - 1];
                        Items[i - 1]     = temp;
                        i /= 2;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Exemple #2
0
        public void add_item(int index, int parent, int fcost, int gcost, bool accessible)
        {
            Current_Id += 1;
            Size++;
            if ((size) > Items.Count)
            {
                Items.Add(new OpenItem {
                    Id = Current_Id, Index = index, Parent = parent, Fcost = fcost, Gcost = gcost, Accessible = accessible
                });
            }
            else
            {
                Items[size - 1] = new OpenItem {
                    Id = Current_Id, Index = index, Parent = parent, Fcost = fcost, Gcost = gcost, Accessible = accessible
                }
            };
            int i = size;

            while (i != 1)
            {
                if (fcost <= Items[i / 2 - 1].Fcost)
                {
                    OpenItem temp = Items[i / 2 - 1];
                    Items[i / 2 - 1] = Items[i - 1];
                    Items[i - 1]     = temp;
                    i /= 2;
                }
                else
                {
                    break;
                }
            }
        }
Exemple #3
0
        private void resort()
        {
            int i1 = 1;

            for (; ;)
            {
                int i2 = i1;
                if (i2 * 2 + 1 <= size)
                {
                    if (Items[i2 - 1].Fcost >= Items[i2 * 2 - 1].Fcost)
                    {
                        i1 = i2 * 2;
                    }
                    if (Items[i1 - 1].Fcost >= Items[i2 * 2].Fcost)
                    {
                        i1 = i2 * 2 + 1;
                    }
                }
                else if (i2 * 2 <= size)
                {
                    if (Items[i2 - 1].Fcost >= Items[i2 * 2 - 1].Fcost)
                    {
                        i1 = i2 * 2;
                    }
                }

                if (i1 != i2)
                {
                    OpenItem temp = Items[i2 - 1];
                    Items[i2 - 1] = Items[i1 - 1];
                    Items[i1 - 1] = temp;
                }
                else
                {
                    break;
                }
            }
        }
        public List <Tuple <int, T> > get_route(T target_loc, T loc, int mov = -1)
        {
            //int start_index = Objects.IndexOf(loc); //@Debug
            //int end_index = Objects.IndexOf(target_loc);
            int start_index = Objects.FindIndex(x => x.SameLocation(loc));
            int end_index   = Objects.FindIndex(x => x.SameLocation(target_loc));

            if (start_index == -1 || end_index == -1)
            {
                return(null);
            }

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

            int temp_parent = -1;

            int  temp_index      = -1;
            int  temp_f          = 0;
            int  temp_g          = 0;
            int  temp_h          = 0;
            bool temp_accessible = true;

            bool route_found = false;

            // Start pathfinding
            temp_g = 0;
            temp_h = distance(start_index, end_index);
            temp_f = temp_g + temp_h;
            open_list.add_item(start_index, temp_parent, temp_f, temp_g, temp_accessible);
            for (; open_list.size > 0;)
            {
                OpenItem lowest_f_item = open_list.get_lowest_f_item();
                temp_index      = lowest_f_item.Index;
                temp_parent     = lowest_f_item.Parent;
                temp_f          = lowest_f_item.Fcost;
                temp_g          = lowest_f_item.Gcost;
                temp_accessible = lowest_f_item.Accessible;

                temp_parent = closed_list.add_item(temp_index, temp_parent, temp_f, temp_g, temp_accessible);
                open_list.remove_open_item();
                if (temp_index == end_index)
                {
                    route_found = true;
                    break;
                }
                else
                {
                    foreach (int test_index in Nodes[temp_index].Adjacent)
                    {
                        // If the checked location isn't the target but is off the map, and off the map is not allowed
                        if (test_index != end_index && (test_index >= Objects.Count || test_index < 0))
                        {
                            continue;
                        }
                        // If the location is already on the closed list
                        if (closed_list.search(test_index) > -1)
                        {
                            continue;
                        }
                        check_tile(test_index, temp_parent, mov, end_index, open_list, closed_list);
                    }
                }
            }
            if (route_found)
            {
                return(closed_list.get_route(temp_parent)
                       .Select(x => new Tuple <int, T>(x, Objects[x]))
                       .Reverse()
                       .ToList());
            }
            return(null);
        }