public PathFindController(Character c, GameLocation location, isAtEnd endFunction, int finalFacingDirection, bool eraseOldPathController, endBehavior endBehaviorFunction, int limit, Point endPoint, bool clearMarriageDialogues = true) { this.limit = limit; character = c; NPC npc = c as NPC; if (npc != null && npc.CurrentDialogue.Count > 0 && npc.CurrentDialogue.Peek().removeOnNextMove) { npc.CurrentDialogue.Pop(); } if (npc != null && clearMarriageDialogues) { if (npc.currentMarriageDialogue.Count > 0) { npc.currentMarriageDialogue.Clear(); } npc.shouldSayMarriageDialogue.Value = false; } this.location = location; this.endFunction = ((endFunction == null) ? new isAtEnd(isAtEndPoint) : endFunction); this.endBehaviorFunction = endBehaviorFunction; if (endPoint == Point.Zero) { endPoint = new Point((int)c.getTileLocation().X, (int)c.getTileLocation().Y); } this.finalFacingDirection = finalFacingDirection; if (!(character is NPC) && !isPlayerPresent() && endFunction == new isAtEnd(isAtEndPoint) && endPoint.X > 0 && endPoint.Y > 0) { character.Position = new Vector2(endPoint.X * 64, endPoint.Y * 64 - 32); return; } pathToEndPoint = findPath(new Point((int)c.getTileLocation().X, (int)c.getTileLocation().Y), endPoint, endFunction, location, character, limit); if (pathToEndPoint == null) { _ = (location is FarmHouse); } }
public static Stack <Point> findPath(Point startPoint, Point endPoint, isAtEnd endPointFunction, GameLocation location, Character character, int limit) { if (Interlocked.Increment(ref _counter) != 1) { throw new Exception(); } try { _openList.Clear(); _closedList.Clear(); PriorityQueue openList = _openList; HashSet <int> closedList = _closedList; int iterations = 0; openList.Enqueue(new PathNode(startPoint.X, startPoint.Y, 0, null), Math.Abs(endPoint.X - startPoint.X) + Math.Abs(endPoint.Y - startPoint.Y)); int layerWidth = location.map.Layers[0].LayerWidth; int layerHeight = location.map.Layers[0].LayerHeight; while (!openList.IsEmpty()) { PathNode currentNode = openList.Dequeue(); if (endPointFunction(currentNode, endPoint, location, character)) { return(reconstructPath(currentNode)); } closedList.Add(currentNode.id); int ng = (byte)(currentNode.g + 1); for (int i = 0; i < 4; i++) { int nx = currentNode.x + Directions[i, 0]; int ny = currentNode.y + Directions[i, 1]; int nid = PathNode.ComputeHash(nx, ny); if (!closedList.Contains(nid)) { if ((nx != endPoint.X || ny != endPoint.Y) && (nx < 0 || ny < 0 || nx >= layerWidth || ny >= layerHeight)) { closedList.Add(nid); } else { PathNode neighbor = new PathNode(nx, ny, currentNode); neighbor.g = (byte)(currentNode.g + 1); if (location.isCollidingPosition(new Rectangle(neighbor.x * 64 + 1, neighbor.y * 64 + 1, 62, 62), Game1.viewport, character is Farmer, 0, glider: false, character, pathfinding: true)) { closedList.Add(nid); } else { int f = ng + (Math.Abs(endPoint.X - nx) + Math.Abs(endPoint.Y - ny)); closedList.Add(nid); openList.Enqueue(neighbor, f); } } } } iterations++; if (iterations >= limit) { return(null); } } return(null); } finally { if (Interlocked.Decrement(ref _counter) != 0) { throw new Exception(); } } }
public static Stack <Point> findPath(Point startPoint, Point endPoint, isAtEnd endPointFunction, GameLocation location, int limit = -1) { if (Interlocked.Increment(ref _counter) != 1) { throw new Exception(); } try { bool endPointIsFarmer = endPoint.X == Game1.player.getTileX() && endPoint.Y == Game1.player.getTileY(); _openList.Clear(); _closedList.Clear(); var openList = _openList; HashSet <int> closedList = _closedList; int iterations = 0; openList.Enqueue(new PathNode(startPoint.X, startPoint.Y, 0, null), Math.Abs(endPoint.X - startPoint.X) + Math.Abs(endPoint.Y - startPoint.Y)); int layerWidth = location.map.Layers[0].LayerWidth; int layerHeight = location.map.Layers[0].LayerHeight; Character character = Game1.player; List <int> searchDirections = SearchDirections(startPoint, endPoint); while (!openList.IsEmpty()) { PathNode currentNode = openList.Dequeue(); if (endPointFunction(currentNode, endPoint, location, character)) { return(reconstructPath(currentNode)); } closedList.Add(currentNode.id); int ng = (byte)(currentNode.g + 1); foreach (int i in searchDirections) { int nx = currentNode.x + Directions[i, 0]; int ny = currentNode.y + Directions[i, 1]; int nid = PathNode.ComputeHash(nx, ny); if (!closedList.Contains(nid)) { bool isWalkable = isTileWalkable(location, nx, ny); bool isEndPoint = nx == endPoint.X && ny == endPoint.Y; bool isOffMap = nx < 0 || ny < 0 || nx >= layerWidth || ny >= layerHeight; if ((!isEndPoint && isOffMap) || !isWalkable) { closedList.Add(nid); } else { PathNode neighbor = new(nx, ny, currentNode); neighbor.g = (byte)(currentNode.g + 1); float f = ng + (Math.Abs(endPoint.X - nx) + Math.Abs(endPoint.Y - ny)); closedList.Add(nid); openList.Enqueue(neighbor, f); } } } iterations++; if (limit >= 0) { if (iterations >= limit) { return(null); } } } return(null); } finally { if (Interlocked.Decrement(ref _counter) != 0) { throw new Exception(); } } }
public MovementController(Character c, GameLocation location, isAtEnd endFunction, int finalFacingDirection, bool eraseOldPathController, endBehavior endBehaviorFunction, int limit, Point endPoint, bool clearMarriageDialogues = true) : base(c, location, endFunction, finalFacingDirection, eraseOldPathController, endBehaviorFunction, limit, endPoint, clearMarriageDialogues) { }