示例#1
0
        public virtual void SetAStarNodeInsert(List <GridLocation> LIST, GridLocation NEWNODE)
        {
            bool added = false;

            for (int i = 0; i < LIST.Count; i++)
            {
                if (LIST[i].fScore > NEWNODE.fScore)
                {
                    //Cant insert at 0, because that would take up the looking at node...
                    LIST.Insert(Math.Max(1, i), NEWNODE);
                    added = true;
                    break;
                }
            }

            if (!added)
            {
                LIST.Add(NEWNODE);
            }
        }
示例#2
0
        public List <Vector2> GetPath(Vector2 START, Vector2 END, bool ALLOWDIAGNALS)
        {
            List <GridLocation> viewable = new List <GridLocation>(), used = new List <GridLocation>();

            List <List <GridLocation> > masterGrid = new List <List <GridLocation> >();


            bool  impassable = false;
            float cost       = 1;

            for (int i = 0; i < slots.Count; i++)
            {
                masterGrid.Add(new List <GridLocation>());
                for (int j = 0; j < slots[i].Count; j++)
                {
                    impassable = slots[i][j].impassable;

                    if (slots[i][j].impassable || slots[i][j].filled)
                    {
                        if (i != (int)END.X || j != (int)END.Y)
                        {
                            impassable = true;
                        }
                    }

                    cost = slots[i][j].cost;

                    masterGrid[i].Add(new GridLocation(new Vector2(i, j), cost, impassable, 99999999));
                }
            }

            viewable.Add(masterGrid[(int)START.X][(int)START.Y]);

            while (viewable.Count > 0 && !(viewable[0].pos.X == END.X && viewable[0].pos.Y == END.Y))
            {
                TestAStarNode(masterGrid, viewable, used, END, ALLOWDIAGNALS);
            }


            List <Vector2> path = new List <Vector2>();

            if (viewable.Count > 0)
            {
                int          currentViewableStart = 0;
                GridLocation currentNode          = viewable[currentViewableStart];

                path.Clear();
                Vector2 tempPos;


                while (true)
                {
                    //Add the difference between the actual grid and the custom grid back in...
                    tempPos = GetPosFromLoc(currentNode.pos) + slotDims / 2;
                    path.Add(new Vector2(tempPos.X, tempPos.Y));

                    if (currentNode.pos == START)
                    {
                        break;
                    }
                    else
                    {
                        if ((int)currentNode.parent.X != -1 && (int)currentNode.parent.Y != -1)
                        {
                            if (currentNode.pos.X == masterGrid[(int)currentNode.parent.X][(int)currentNode.parent.Y].pos.X && currentNode.pos.Y == masterGrid[(int)currentNode.parent.X][(int)currentNode.parent.Y].pos.Y)
                            {
                                //Current node points to its self
                                currentNode = viewable[currentViewableStart];
                                currentViewableStart++;
                            }


                            currentNode = masterGrid[(int)currentNode.parent.X][(int)currentNode.parent.Y];
                        }
                        else
                        {
                            //Node is off grid...
                            currentNode = viewable[currentViewableStart];
                            currentViewableStart++;
                        }
                    }
                }



                path.Reverse();

                if (path.Count > 1)
                {
                    path.RemoveAt(0);
                }
            }

            return(path);
        }
示例#3
0
        public void SetAStarNode(List <GridLocation> viewable, List <GridLocation> used, GridLocation nextNode, Vector2 nextParent, float d, Vector2 target, float DISTMULT)
        {
            float f         = d;
            float addedDist = (nextNode.cost * DISTMULT);



            //Add item
            if (!nextNode.isViewable && !nextNode.hasBeenUsed)
            {
                //viewable.Add(new AStarNode(nextParent, f, new Vector2(nextNode.Pos.X, nextNode.Pos.Y), nextNode.CurrentDist + 1, nextNode.Cost, nextNode.Impassable));

                nextNode.SetNode(nextParent, f, d + addedDist);
                nextNode.isViewable = true;

                SetAStarNodeInsert(viewable, nextNode);
            }
            //Node is in viewable, so check if Fscore needs revised
            else if (nextNode.isViewable)
            {
                if (f < nextNode.fScore)
                {
                    nextNode.SetNode(nextParent, f, d + addedDist);
                }
            }
        }