/// <summary>
 /// Reset the queue so no steps to walk.
 /// </summary>
 public void Reset()
 {
     // Set the point to the current location so the character doesn't move.
     points[0] = new MovementPoint(
         this.npc.Location.X,
         this.npc.Location.Y, -1);
     this.readPosition = writePosition = 1;
 }
 /// <summary>
 /// Constructs a new walking queue class.
 /// </summary>
 public WalkingQueue(Npc npc)
 {
     this.npc = npc;
     for (int i = 0; i < WalkingQueue.Capacity; i++)
     {
         points[i] = new MovementPoint(0, 0, -1);
     }
     Reset();
 }
示例#3
0
        /// <summary>
        /// Constructs a new walking queue class.
        /// </summary>
        /// <param name="character">The character to contruct the component for.</param>
        public WalkingQueue(Character character)
        {
            this.character  = character;
            this.RunEnergy  = 100;
            this.Running    = false;
            this.RunToggled = false;

            for (int i = 0; i < WalkingQueue.Capacity; i++)
            {
                points[i] = new MovementPoint(0, 0, -1);
            }
            Reset();
        }
示例#4
0
        public void UpdateAllTargets(CharInfo character)
        {
            if (character)
            {
                allTargets    = normalTargets = UpdateNormalTargets(character);
                customTargets = UpdateCustomTargets(character);
                customTargets.ForEach(target => allTargets.Add(target.GetTarget()));
                centerPoint = new CenterPoint(character);
                if (centerPoint != null && centerPoint.GetPoint())
                {
                    allTargets.Add(centerPoint.GetPoint());
                }
                movementPoint = new MovementPoint(character);
                allTargets.Add(movementPoint.GetPoint());
            }
            else
            {
                if (centerPoint != null)
                {
                    GameObject.Destroy(centerPoint.GetPoint());
                    centerPoint = null;
                }

                if (movementPoint != null)
                {
                    GameObject.Destroy(movementPoint.GetPoint());
                    movementPoint = null;
                }

                for (int i = 0; i < customTargets.Count; i++)
                {
                    GameObject.Destroy(customTargets[i].GetTarget());
                }

                allTargets    = new List <GameObject>();
                normalTargets = new List <GameObject>();
                customTargets = new List <CustomTarget>();
            }
        }
        /// <summary>
        /// Gets the next point.
        /// </summary>
        /// <returns>Returns the point's direction.</returns>
        private sbyte GetNextPoint()
        {
            // Nothing to read.
            if (this.readPosition == this.writePosition)
            {
                return(-1);
            }

            MovementPoint mp  = points[this.readPosition++];
            sbyte         dir = DirectionUtilities.CalculateDirection(this.npc.Location.X, this.npc.Location.Y, mp.X, mp.Y);


            /*
             * You cannot search though an array with a negative number,
             * so we must check if the direction is higher than -1.
             */
            if (dir != -1)
            {
                dir >>= 1;
                this.npc.Location = Location.Create(mp.X, mp.Y, this.npc.Location.Z);
            }
            return(dir);
        }
        private List <MovementPoint> ParseSubtitleFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return(null);
            }

            var subtitles = File.ReadAllText(fileName);

            var movementPointRegExp = @"\d{6}\.\d+,A,\d{4}(\.\d*)?,N,\d{5}(\.\d*)?,E,\d*(\.\d*)?,\d*(\.\d*)?,\d{6},,,.{4}";
            var matches             = Regex.Matches(subtitles, movementPointRegExp);
            List <MovementPoint> en = new List <MovementPoint>();

            foreach (Match m in matches)
            {
                string        g   = m.Value;
                MovementPoint mov = ParseMovementPoint(g);
                pointsList.Add(mov);
                en.Add(mov);
            }

            return(en);
        }
示例#7
0
    public List <MovementPoint> FindPath(Vector2i source, Vector2i target, int?maxRange = null)
    {
        MovementPoint entryPoint = new MovementPoint {
            position = target,
            origin   = null,
            distance = Chebyshev(target, source),
            length   = 0
        };

        List <MovementPoint> waiting = new List <MovementPoint>();
        List <MovementPoint> done    = new List <MovementPoint>();

        waiting.Add(entryPoint);

        bool isFinished = false;

        while (!isFinished)
        {
            // iterate
            if (waiting.Count == 0)
            {
                // The waiting list is empty. That means no path can be found.
                isFinished = true;
                return(null);
            }

            List <MovementPoint> newWaiting = new List <MovementPoint>();

            waiting.Sort(delegate(MovementPoint a, MovementPoint b) {
                if (a.distance < b.distance)
                {
                    return(-1);
                }
                if (b.distance < a.distance)
                {
                    return(1);
                }
                return(0);
            });
            waiting.Reverse();

            for (int i = waiting.Count - 1; i > -1; i--)
            {
                MovementPoint current = waiting[i];

                // check if the currently checked point is the source, regenerate and return path if true
                if (current.position == source)
                {
                    isFinished = true;
                    // retrace path and return it

                    List <MovementPoint> path       = new List <MovementPoint>();
                    MovementPoint        breadCrumb = current;
                    while (breadCrumb.position != target)
                    {
                        path.Add(breadCrumb);
                        breadCrumb = breadCrumb.origin;
                    }
                    path.Add(entryPoint);
                    return(path);
                }

                //Debug.Log("current.length: " + current.length.ToString() + "; maxRange: " + maxRange.ToString());
                if (maxRange == null || current.length < maxRange)
                {
                    List <Vector2i> neighbours = GetNeighbours(current.position);

                    foreach (var neighbour in neighbours)
                    {
                        if (!IsPassable(neighbour))
                        {
                            continue;
                        }

                        if (waiting.Find(x => x.position == neighbour) != null)
                        {
                            continue;
                        }
                        if (done.Find(x => x.position == neighbour) != null)
                        {
                            continue;
                        }

                        MovementPoint newPoint = new MovementPoint {
                            position = neighbour,
                            origin   = current,
                            distance = Chebyshev(neighbour, source),
                            length   = current.length + 1
                        };

                        newWaiting.Add(newPoint);
                    }
                }

                waiting.Remove(current);
                done.Add(current);
            }

            waiting.AddRange(newWaiting);
            newWaiting.Clear();
            if (waiting.Count > 50)
            {
                waiting.RemoveRange(50, waiting.Count - 50);
            }
        }

        return(null);
    }