Jump to content
  • 1

How to generate diagonal values with the Npc pathfinder


Shenmue

Question

Actually I have my NPCs walking in diagonal, but when they have a target, they go back to 4 movements. I think it's due to the "search" method in "Intersect.Server.Entities.Pathfinding.SpatialAstar" and I was wondering how to generate a path with values that has diagonal directions.

 

Can someone help me with this?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Solution found. Have to configure more possibilities to "StoreNeighborNodes" methode in "Server/Entities/Pathfinding/SpatialAStar.cs" and the same for GetMove() methode in "Server/Entities/Pathfinding/Pathfinder.cs"

 

Spoiler

        private void StoreNeighborNodes(PathNode inAround, PathNode[] inNeighbors)
        {
            var x = inAround.X;
            var y = inAround.Y;

            if (y > 0 && x > 0)
            {
                inNeighbors[0] = mSearchSpace[x - 1, y - 1]; // UpLeft
            }
            else
            {
                inNeighbors[0] = null;
            }

            if (y > 0 && x < Width - 1)
            {
                inNeighbors[2] = mSearchSpace[x + 1, y - 1]; // UpRight
            }
            else
            {
                inNeighbors[2] = null;
            }

            if (y < Height - 1 && x > 0)
            {
                inNeighbors[5] = mSearchSpace[x - 1, y + 1]; // DownLeft
            }
            else
            {
                inNeighbors[5] = null;
            }

            if (y < Height - 1 && x < Width - 1)
            {
                inNeighbors[7] = mSearchSpace[x + 1, y + 1]; // DownRight
            }
            else
            {
                inNeighbors[7] = null;
            }

            if (y > 0)
            {
                inNeighbors[1] = mSearchSpace[x, y - 1]; // Up
            }
            else
            {
                inNeighbors[1] = null;
            }


            if (x > 0) 
            {
                inNeighbors[3] = mSearchSpace[x - 1, y]; // Left
            }
            else
            {
                inNeighbors[3] = null;
            }

            if (x < Width - 1) 
            {
                inNeighbors[4] = mSearchSpace[x + 1, y]; // Right
            }
            else
            {
                inNeighbors[4] = null;
            }


            if (y < Height - 1)
            {
                inNeighbors[6] = mSearchSpace[x, y + 1]; // Down
            }
            else
            {
                inNeighbors[6] = null;
            }


        }

 

 

Spoiler


        public sbyte GetMove()
        {
            if (mPath == null)
            {
                return -1;
            }

            var enm = mPath.GetEnumerator();
            while (enm.MoveNext())
            {
                if (enm.Current.X - Options.MapWidth == mEntity.X && enm.Current.Y - Options.MapHeight == mEntity.Y)
                {
                    if (enm.MoveNext())
                    {
                        var newX = enm.Current.X - Options.MapWidth;
                        var newY = enm.Current.Y - Options.MapHeight;
                        if (mEntity.X < newX && mEntity.Y == newY)
                        {
                            enm.Dispose();

                            return (int) Directions.Right;
                        }
                        else if (mEntity.X > newX && mEntity.Y == newY)
                        {
                            enm.Dispose();

                            return (int) Directions.Left;
                        }
                        else if (mEntity.Y < newY && mEntity.X == newX)
                        {
                            enm.Dispose();

                            return (int) Directions.Down;
                        }
                        else if (mEntity.Y > newY && mEntity.X == newX)
                        {
                            enm.Dispose();

                            return (int) Directions.Up;
                        }
                        else if (mEntity.Y > newY && mEntity.X > newX)
                        {
                            enm.Dispose();

                            return (int)Directions.UpLeft;
                        }
                        else if (mEntity.Y > newY && mEntity.X < newX)
                        {
                            enm.Dispose();

                            return (int)Directions.UpRight;
                        }
                        else if (mEntity.Y < newY && mEntity.X > newX)
                        {
                            enm.Dispose();

                            return (int)Directions.DownLeft;
                        }
                        else if (mEntity.Y < newY && mEntity.X < newX)
                        {
                            enm.Dispose();

                            return (int)Directions.DownRight;
                        }
                    }
                }
            }

            enm.Dispose();

            return -1;
        }

    }

 

 

Link to comment
Share on other sites

×
×
  • Create New...