Exemplo n.º 1
0
        private bool Mark(int x, int y, FlowFieldPath foundPath, LinkedList <Point2D> openList, ref int foundAmount)
        {
            int pathId = x + y * Width;

            if (pathId < 0 || pathId >= Height * Width || pathfinding[pathId] == 0)
            {
                return(true);
            }
            if (foundPath.Get(pathId) == 1)
            {
                foundAmount++;
            }
            foundPath.Set(pathId, 255);
            openList.AddLast(new Point2D(x, y));
            return(false);
        }
Exemplo n.º 2
0
        public FlowFieldPath FindPath(List <Point2D> points, Point2D destination)
        {
            foundPath = new FlowFieldPath(Width * Height);

            int DestX = (int)(destination.X / CellSize);
            int DestY = (int)(destination.Y / CellSize);

            LinkedList <Point2D> openList = new LinkedList <Point2D>();

            Point2D current = new Point2D(DestX, DestY);

            openList.AddLast(current);



            int x, y;

            foreach (Point2D p in points)
            {
                var pw = p;
                x = (int)(pw.X / CellSize);
                y = (int)(pw.Y / CellSize);
                foundPath.Set(x + y * Width, 1);
            }


            int tmpX, tmpY, pathId;

            int  foundAmount = 0;
            bool stopAdding  = false;

            // Experimental poitn of view style ( like in supcom 2 ) flowfield optimization
            //------------------------------------------------------------------------------------
            // Disabled because it doesn't respect diagonal obstruction rule

            /*if (false) {
             *  int limit = 0, maxLimit = 20;
             *  foreach (Point2D p in points) {
             *      x = Math.Abs((int)(p.X / CellSize) - DestX);
             *      y = Math.Abs((int)(p.Y / CellSize) - DestY);
             *      if (x > limit && y > limit) limit = Math.Min(Math.Max(x, y), maxLimit);
             *  }
             *
             *  for (int xtmp = -limit; xtmp <= limit; xtmp++) {
             *      for (int ytmp = -limit; ytmp <= limit; ytmp++) {
             *          pathId = (DestX + xtmp) + (DestY + ytmp) * Width;
             *          if (pathId >= 0 && pathId < Height * Width && (xtmp == -limit || ytmp == -limit || xtmp == limit || ytmp == limit))
             *              Line(DestX, DestY, DestX + xtmp, DestY + ytmp, foundPath, openList, ref foundAmount);
             *      }
             *  }
             *
             *  if (foundAmount == points.Count) { openList.Clear(); }
             * }*/
            //------------------------------------------------------------------------------------

            while (openList.Count > 0)
            {
                current = openList.First.Value;
                openList.RemoveFirst();

                //Find neighbours
                byte bitmask = pathfinding[current.X + current.Y * Width];
                for (int i = 0; i < 8; i++)
                {
                    if ((bitmask & (1 << i)) == (1 << i))
                    {
                        tmpX   = current.X + Direction[i, 0];
                        tmpY   = current.Y + Direction[i, 1];
                        pathId = tmpX + tmpY * Width;

                        byte found = foundPath.Get(pathId);
                        if (found == 0 || found == 1)
                        {
                            if (!stopAdding)
                            {
                                openList.AddLast(new Point2D(tmpX, tmpY));
                            }
                            foundPath.Set(pathId, (byte)(2 + i));

                            if (found == 1)
                            {
                                foundAmount++;
                                if (foundAmount == points.Count)
                                {
                                    stopAdding = true;
                                }
                            }
                        }

                        /*} else {
                         *  tmpX = current.X + Direction[i, 0];
                         *  tmpY = current.Y + Direction[i, 1];
                         *  pathId = tmpX + tmpY * width;
                         *  byte found = foundPath[pathId];
                         *  if (found == 1) {
                         *      foundAmount++;
                         *      if (foundAmount == points.Count) stopAdding = true;
                         *  }else if (found<100){
                         *      if (pathId>=0 && pathId<foundPath.Length) foundPath[pathId] = (byte)(102+i);
                         *  }
                         */
                    }
                }
            }

            foundPath.Set(DestX + DestY * Width, 255);

            return(foundPath);
        }