private PathGeometry GetPathGeometry(Point position) { PathGeometry geometry = new PathGeometry(); ConnectorOrientation targetOrientation; if (HitConnector != null) { targetOrientation = HitConnector.Orientation; } else { targetOrientation = ConnectorOrientation.None; } List <Point> pathPoints = PathFinderHelper.GetPathFinder(this.designerCanvas.PathFinder).GetConnectionLine(sourceConnector.GetInfo(), position, targetOrientation); if (pathPoints.Count > 0) { PathFigure figure = new PathFigure(); figure.StartPoint = pathPoints[0]; pathPoints.Remove(pathPoints[0]); figure.Segments.Add(new PolyLineSegment(pathPoints, true)); geometry.Figures.Add(figure); } return(geometry); }
private PathGeometry UpdatePathGeometry(Point position) { PathGeometry geometry = new PathGeometry(); ConnectorOrientation targetOrientation; if (HitConnector != null) { targetOrientation = HitConnector.Orientation; } else { targetOrientation = dragConnector.Orientation; } List <Point> linePoints = PathFinderHelper.GetPathFinder(this.connection.pathFinder).GetConnectionLine(fixConnector.GetInfo(), position, targetOrientation); if (linePoints.Count > 0) { PathFigure figure = new PathFigure(); figure.StartPoint = linePoints[0]; linePoints.Remove(linePoints[0]); figure.Segments.Add(new PolyLineSegment(linePoints, true)); geometry.Figures.Add(figure); } return(geometry); }
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)); } }
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); }
/// <summary> /// Create smaller grid for pathfinding - for non-async calls /// </summary> /// <param name="designer">From this viewmodel is grid created</param> /// <param name="step">Grid row/col step</param> /// <returns>Created grid</returns> public static Grid CreateMinifiedGridForPathFindingSync(DatabaseModelDesignerViewModel designer, int step) { var rects = GetTableRectangles(designer.TableViewModels, step).Select(s => { var t = s.Y / step; var l = s.X / step; var r = s.Right / step; var b = s.Bottom / step; return(new Rectangle(l, t, r - l, b - t)); }); return(PathFinderHelper.CreateGrid((int)(designer.CanvasWidth / step), (int)designer.CanvasHeight / step, rects)); }
/// <summary> /// Create smaller grid for pathfinding /// </summary> /// <param name="designer">From this viewmodel is grid created</param> /// <param name="step">Grid row/col step</param> /// <returns>Task for async execution => Created grid</returns> public static async Task <Grid> CreateMinifiedGridForPathFinding(DatabaseModelDesignerViewModel designer, int step) { var rects = GetTableRectangles(designer.TableViewModels, step).Select(s => { var t = s.Y / step; var l = s.X / step; var r = s.Right / step; var b = s.Bottom / step; return(new Rectangle(l, t, r - l, b - t)); }); var res = await Task.Factory.StartNew(() => PathFinderHelper.CreateGrid((int)(designer.CanvasWidth / step), (int)designer.CanvasHeight / step, rects)); return(res); }
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; } }
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"); } }
private void UpdatePathGeometry() { if (Source != null && Sink != null) { PathGeometry geometry = new PathGeometry(); pointsList = PathFinderHelper.GetPathFinder(this.pathFinder).GetConnectionLine(Source.GetInfo(), Sink.GetInfo(), true); if (pointsList.Count > 0) { PathFigure figure = new PathFigure(); figure.IsClosed = false; figure.StartPoint = pointsList[0]; pointsList.Remove(pointsList[0]); figure.Segments.Add(new PolyLineSegment(pointsList, true)); geometry.Figures.Add(figure); this.PathGeometry = geometry; this.Points = pointsList; } } }
internal PathFinder(IResourceSystem fileSystem) { _helper = PathFinderHelper.GetInstance(fileSystem); }
/// <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); }
public void TestRoundToNearestPowerOfTwoWithNonPositiveNRaisesException() { Assert.Throws(typeof(ArgumentOutOfRangeException), () => PathFinderHelper.RoundToNearestPowerOfTwo(-27)); Assert.Throws(typeof(ArgumentOutOfRangeException), () => PathFinderHelper.RoundToNearestPowerOfTwo(-1)); Assert.Throws(typeof(ArgumentOutOfRangeException), () => PathFinderHelper.RoundToNearestPowerOfTwo(0)); }