Exemplo n.º 1
0
        public void TestRoundToNearestPowerOfTwoPositiveCases()
        {
            Dictionary <int, int> expectedAndActual = new Dictionary <int, int>();

            expectedAndActual.Add(1, 1);
            expectedAndActual.Add(2, 2);
            expectedAndActual.Add(3, 4);
            expectedAndActual.Add(4, 4);
            expectedAndActual.Add(5, 8);
            expectedAndActual.Add(6, 8);
            expectedAndActual.Add(7, 8);
            expectedAndActual.Add(8, 8);
            expectedAndActual.Add(9, 16);
            expectedAndActual.Add(10, 16);

            expectedAndActual.Add(100, 128);
            expectedAndActual.Add(253, 256);
            expectedAndActual.Add(261, 512);
            expectedAndActual.Add(777, 1024);
            expectedAndActual.Add(1111, 2048);
            expectedAndActual.Add(2227, 4096);
            expectedAndActual.Add(6086, 8192);
            expectedAndActual.Add(11398, 16384);
            expectedAndActual.Add(31313, 32768);

            foreach (int key in expectedAndActual.Keys)
            {
                Assert.That(PathFinderHelper.RoundToNearestPowerOfTwo(key) == expectedAndActual[key],
                            "Power of two for " + key + " was supposed to be " + expectedAndActual[key] + " but turned out to be " + PathFinderHelper.RoundToNearestPowerOfTwo(key));
            }
        }
Exemplo n.º 2
0
        private static bool LoadRecord(Zone zone, NavigationRecordType type)
        {
            string file   = AppDomain.CurrentDomain.BaseDirectory + @"\assets\" + Convert.ToInt32(zone) + type;
            int    zoneId = (int)zone;
            string line;

            // create a new mesh
            if (type == NavigationRecordType.Mesh)
            {
                if (!_grids.ContainsKey(zoneId))
                {
                    _grids.Add(zoneId, new byte[PathFinderHelper.RoundToNearestPowerOfTwo(4000), PathFinderHelper.RoundToNearestPowerOfTwo(4000)]);
                }
                for (int i = 0; i < 4096; i++)
                {
                    for (int j = 0; j < 4096; j++)
                    {
                        _grids[zoneId][i, j] = PathFinderHelper.BLOCKED_TILE;
                    }
                }
            }
            if (File.Exists(file))
            {
                StreamReader stream = new StreamReader(file);
                while ((line = stream.ReadLine()) != null)
                {
                    string[] token = line.Split(',');
                    switch (type)
                    {
                    case NavigationRecordType.Mesh:
                    {
                        int x = int.Parse(token[2], CultureInfo.InvariantCulture) + _offset;
                        int z = int.Parse(token[4], CultureInfo.InvariantCulture) + _offset;
                        _grids[zoneId][x, z] = PathFinderHelper.EMPTY_TILE;
                        break;
                    }

                    case NavigationRecordType.Blackspot:
                    {
                        _blackspots[zoneId].Clear();
                        int x      = int.Parse(token[1], CultureInfo.InvariantCulture);
                        int z      = int.Parse(token[3], CultureInfo.InvariantCulture);
                        int radius = int.Parse(token[4]);
                        _blackspots[zoneId].Add(new Blackspot {
                                X = x, Z = z, Radius = radius
                            });
                        break;
                    }

                    case NavigationRecordType.Hotspot:
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(type), type, null);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        public PathFindingLevel(MultiLevelWorld world)
            : base(world)
        {
            int width  = PathFinderHelper.RoundToNearestPowerOfTwo(Tileset.TILESET_WIDTH);
            int height = PathFinderHelper.RoundToNearestPowerOfTwo(Tileset.TILESET_HEIGHT);

            this.m_PathFindingGrid = new byte[width, height];
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    this.m_PathFindingGrid[x, y] = (byte)PathFinderHelper.EMPTY_TILE;
                }
            }
        }
        public AStarStructPathFinder(byte[,] grid, int width, int height)
        {
            _width  = width;
            _height = height;
            int newWidth  = PathFinderHelper.RoundToNearestPowerOfTwo(width);
            int newHeight = PathFinderHelper.RoundToNearestPowerOfTwo(height);

            _grid  = new NodeStruct[newWidth * newHeight];
            _queue = new PriorityQueue <NodeStruct>();

            for (int i = 0; i < _width; i++)
            {
                _grid[GetPosition(i, _width)].State = NodeState.Obstacle;
                _grid[GetPosition(_width, i)].State = NodeState.Obstacle;
            }
        }
Exemplo n.º 5
0
 public void CreateGrid()
 {
     try
     {
         Grid = new byte[PathFinderHelper.RoundToNearestPowerOfTwo(2000), PathFinderHelper.RoundToNearestPowerOfTwo(2000)];
         for (int i = 0; i < 2000; i++)
         {
             for (int j = 0; j < 2000; j++)
             {
                 Grid[i, j] = PathFinderHelper.BLOCKED_TILE;
             }
         }
     }
     catch (Exception ex)
     {
         Character.Logger.LogFile(ex.Message, "Nav");
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Loads coordinates from a file.
        /// </summary>
        /// <returns></returns>
        public static bool LoadCoords(Zone zone)
        {
            List <string> files = new List <string>();

            Grid = new byte[PathFinderHelper.RoundToNearestPowerOfTwo(4000), PathFinderHelper.RoundToNearestPowerOfTwo(4000)];
            for (int i = 0; i < 4096; i++)
            {
                for (int j = 0; j < 4096; j++)
                {
                    Grid[i, j] = PathFinderHelper.BLOCKED_TILE;
                }
            }

            Waypoints.Clear();
            string loadFile  = AppDomain.CurrentDomain.BaseDirectory + @"\assets\global.mesh";
            string loadFileM = AppDomain.CurrentDomain.BaseDirectory + @"\assets\" + (int)zone + ".mesh";

            files.Add(loadFile);
            files.Add(loadFileM);
            int caught = 0;

            foreach (string f in files)
            {
                if (File.Exists(f))
                {
                    string line;
                    // Read the file and display it line by line.
                    System.IO.StreamReader file = new System.IO.StreamReader(f);
                    while ((line = file.ReadLine()) != null)
                    {
                        string[] token = line.Split(',');
                        if (Convert.ToInt32(token[1]) == (int)zone)
                        {
                            bool inblackspot = false;
                            foreach (Blacklist b in Blacklists)
                            {
                                if (InCircle((int)b.Waypoint.X, (int)b.Waypoint.Z, Convert.ToInt32(token[2]),
                                             Convert.ToInt32(token[4]), Convert.ToInt32(b.Radius)))
                                {
                                    inblackspot = true;
                                    caught++;
                                    //WriteLog("In Blackpost (Ignoring!!): " + token[2] + " / " + token[4]);
                                }
                            }
                            if (!inblackspot)
                            {
                                Waypoints.Add(new Node
                                {
                                    X = float.Parse((token[2]), CultureInfo.InvariantCulture),
                                    Y = float.Parse((token[3]), CultureInfo.InvariantCulture),
                                    Z = float.Parse((token[4]), CultureInfo.InvariantCulture)
                                });
                                Grid[int.Parse((token[2]), CultureInfo.InvariantCulture) + offset,
                                     int.Parse((token[4]), CultureInfo.InvariantCulture) + offset] = PathFinderHelper.EMPTY_TILE;
                            }
                        }
                    }
                    //WriteLog($"Excluded {caught} nodes, due to being in blackspots.");
                    WriteLog($"Loaded {Waypoints.Count} ({f.Split(Convert.ToChar(@"\"))[f.Split(Convert.ToChar(@"\")).Count() - 1].Replace(".mesh", "")}) nodes.");
                    file.Close();
                }
            }
            return(false);
        }
Exemplo n.º 7
0
 public void TestRoundToNearestPowerOfTwoWithNonPositiveNRaisesException()
 {
     Assert.Throws(typeof(ArgumentOutOfRangeException), () => PathFinderHelper.RoundToNearestPowerOfTwo(-27));
     Assert.Throws(typeof(ArgumentOutOfRangeException), () => PathFinderHelper.RoundToNearestPowerOfTwo(-1));
     Assert.Throws(typeof(ArgumentOutOfRangeException), () => PathFinderHelper.RoundToNearestPowerOfTwo(0));
 }