Пример #1
0
        public bool HasGlobalVision(Vector2 coords)
        {
            Vector2            vector = TranslateToNavGrid(coords);
            NavigationGridCell cell   = GetCell((short)vector.X, (short)vector.Y);

            return(cell != null && cell.HasFlag(NavigationGridCellFlags.HAS_GLOBAL_VISION));
        }
Пример #2
0
        public bool IsBrush(Vector2 coords)
        {
            Vector2 vector = TranslateToNavGrid(new Vector2 {
                X = coords.X, Y = coords.Y
            });
            NavigationGridCell cell = GetCell((short)vector.X, (short)vector.Y);

            return(cell != null && cell.HasFlag(NavigationGridCellFlags.HAS_GRASS));
        }
Пример #3
0
        public bool IsSeeThrough(Vector2 coords)
        {
            Vector2 vector = TranslateToNavGrid(new Vector2 {
                X = coords.X, Y = coords.Y
            });
            NavigationGridCell cell = GetCell((short)vector.X, (short)vector.Y);

            return(cell != null && cell.HasFlag(NavigationGridCellFlags.SEE_THROUGH));
        }
Пример #4
0
        public bool IsWalkable(Vector2 coords)
        {
            Vector2 vector = TranslateToNavGrid(new Vector2 {
                X = coords.X, Y = coords.Y
            });
            NavigationGridCell cell = GetCell((short)vector.X, (short)vector.Y);

            return(cell != null && !cell.HasFlag(NavigationGridCellFlags.NOT_PASSABLE));
        }
Пример #5
0
        private List <NavigationGridCell> GetCellNeighbors(NavigationGridCell cell)
        {
            List <NavigationGridCell> neighbors = new List <NavigationGridCell>();

            for (short dirY = -1; dirY <= 1; dirY++)
            {
                for (short dirX = -1; dirX <= 1; dirX++)
                {
                    short nx = (short)(cell.Locator.X + dirX);
                    short ny = (short)(cell.Locator.Y + dirY);
                    NavigationGridCell neighborCell = GetCell(nx, ny);
                    if (neighborCell != null)
                    {
                        neighbors.Add(neighborCell);
                    }
                }
            }
            return(neighbors);
        }
Пример #6
0
        public NavigationGrid(Stream stream)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                byte   major = br.ReadByte();
                ushort minor = major != 2 ? br.ReadUInt16() : (ushort)0;
                if (major != 2 && major != 3 && major != 5 && major != 7)
                {
                    throw new Exception(string.Format("Unsupported Navigation Grid Version: {0}.{1}", major, minor));
                }

                this.MinGridPosition = br.ReadVector3();
                this.MaxGridPosition = br.ReadVector3();

                this.CellSize   = br.ReadSingle();
                this.CellCountX = br.ReadUInt32();
                this.CellCountY = br.ReadUInt32();

                this.Cells      = new NavigationGridCell[this.CellCountX * this.CellCountY];
                this.RegionTags = new uint[this.CellCountX * this.CellCountY];

                if (major == 2 || major == 3 || major == 5)
                {
                    for (int i = 0; i < this.Cells.Length; i++)
                    {
                        this.Cells[i] = NavigationGridCell.ReadVersion5(br, i);
                    }

                    if (major == 5)
                    {
                        for (int i = 0; i < this.RegionTags.Length; i++)
                        {
                            this.RegionTags[i] = br.ReadUInt16();
                        }
                    }
                }
                else if (major == 7)
                {
                    for (int i = 0; i < this.Cells.Length; i++)
                    {
                        this.Cells[i] = NavigationGridCell.ReadVersion7(br, i);
                    }
                    for (int i = 0; i < this.Cells.Length; i++)
                    {
                        this.Cells[i].SetFlags((NavigationGridCellFlags)br.ReadUInt16());
                    }

                    for (int i = 0; i < this.RegionTags.Length; i++)
                    {
                        this.RegionTags[i] = br.ReadUInt32();
                    }
                }

                if (major >= 5)
                {
                    uint groupCount = major == 5 ? 4u : 8u;
                    this.RegionTagTable = new NavigationRegionTagTable(br, groupCount);
                }

                this.SampledHeightsCountX   = br.ReadUInt32();
                this.SampledHeightsCountY   = br.ReadUInt32();
                this.SampledHeightsDistance = br.ReadVector2();
                this.SampledHeights         = new float[this.SampledHeightsCountX * this.SampledHeightsCountY];
                for (int i = 0; i < this.SampledHeights.Length; i++)
                {
                    this.SampledHeights[i] = br.ReadSingle();
                }

                this.HintGrid = new NavigationHintGrid(br);

                this.MapWidth    = this.MaxGridPosition.X + this.MinGridPosition.X;
                this.MapHeight   = this.MaxGridPosition.Z + this.MinGridPosition.Z;
                this.MiddleOfMap = new Vector2(this.MapWidth / 2, this.MapHeight / 2);
                this.TranslationMaxGridPosition = new Vector3
                {
                    X = this.CellCountX / (this.MaxGridPosition.X - this.MinGridPosition.X),
                    Z = this.CellCountY / (this.MaxGridPosition.Z - this.MinGridPosition.Z)
                };
            }
        }
Пример #7
0
 public bool IsAnythingBetween(NavigationGridCell a, NavigationGridCell b)
 {
     return(IsAnythingBetween(a.Locator, b.Locator));
 }
Пример #8
0
        public List <Vector2> GetPath(Vector2 from, Vector2 to)
        {
            List <Vector2> returnList = new List <Vector2>()
            {
                from
            };

            Vector2            vectorFrom = TranslateToNavGrid(from);
            NavigationGridCell cellFrom   = GetCell((short)vectorFrom.X, (short)vectorFrom.Y);

            Vector2            vectorTo = TranslateToNavGrid(to);
            NavigationGridCell goal     = GetCell((short)vectorTo.X, (short)vectorTo.Y);

            if (cellFrom != null && goal != null)
            {
                SimplePriorityQueue <Stack <NavigationGridCell> > priorityQueue = new SimplePriorityQueue <Stack <NavigationGridCell> >();
                Stack <NavigationGridCell>           start      = new Stack <NavigationGridCell>();
                Dictionary <int, NavigationGridCell> closedList = new Dictionary <int, NavigationGridCell>();
                Stack <NavigationGridCell>           path       = null;

                start.Push(cellFrom);
                priorityQueue.Enqueue(start, NavigationGridCell.Distance(cellFrom, goal));
                closedList.Add(cellFrom.ID, cellFrom);

                // while there are still paths to explore
                while (true)
                {
                    if (!priorityQueue.TryFirst(out _))
                    {
                        // no solution
                        return(null);
                    }

                    float currentCost = priorityQueue.GetPriority(priorityQueue.First);
                    priorityQueue.TryDequeue(out path);

                    NavigationGridCell cell = path.Peek();

                    currentCost -= (NavigationGridCell.Distance(cell, goal) + cell.Heuristic); // decrease the heuristic to get the cost

                    // found the min solution return it (path)
                    if (cell.ID == goal.ID)
                    {
                        break;
                    }

                    foreach (NavigationGridCell neighborCell in GetCellNeighbors(cell))
                    {
                        // if the neighbor in the closed list - skip
                        if (closedList.TryGetValue(neighborCell.ID, out _))
                        {
                            continue;
                        }

                        // not walkable - skip
                        if (neighborCell.HasFlag(NavigationGridCellFlags.NOT_PASSABLE) || neighborCell.HasFlag(NavigationGridCellFlags.SEE_THROUGH))
                        {
                            closedList.Add(neighborCell.ID, neighborCell);
                            continue;
                        }

                        // calculate the new path and cost +heuristic and add to the priority queue
                        Stack <NavigationGridCell> npath = new Stack <NavigationGridCell>(new Stack <NavigationGridCell>(path));

                        npath.Push(neighborCell);
                        // add 1 for every cell used
                        priorityQueue.Enqueue(npath, currentCost + 1 + neighborCell.Heuristic + neighborCell.ArrivalCost + neighborCell.AdditionalCost
                                              + NavigationGridCell.Distance(neighborCell, goal));
                        closedList.Add(neighborCell.ID, neighborCell);
                    }
                }

                NavigationGridCell[] pathArray = path.ToArray();
                Array.Reverse(pathArray);

                List <NavigationGridCell> pathList = SmoothPath(new List <NavigationGridCell>(pathArray));

                pathList.RemoveAt(0); // removes the first point

                foreach (NavigationGridCell navGridCell in pathList.ToArray())
                {
                    returnList.Add(TranslateFrmNavigationGrid(navGridCell.Locator));
                }

                return(returnList);
            }

            return(null);
        }
Пример #9
0
 public static int Distance(NavigationGridCell a, NavigationGridCell b)
 {
     return(NavigationGridLocator.Distance(a.Locator, b.Locator));
 }