private static AStarNode aStarSearch(Location start, Location end, Level level) { HashSet<AStarNode> open = new HashSet<AStarNode>(); HashSet<AStarNode> closed = new HashSet<AStarNode>(); AStarNode startNode = new AStarNode(start); startNode.g = 0; startNode.h = start.distance(end); open.Add(startNode); while (open.Count > 0) { AStarNode q = new AStarNode(new Location(0, 0)); double min = 10000; foreach (AStarNode node in open) { if (node.f() < min) { min = node.f(); q = node; } } open.Remove(q); Direction[] directions = {Direction.North, Direction.South, Direction.East, Direction.West}; for (int i = 0; i < 4; i++) { Location adj = q.location.getAdjLocation(directions[i]); if (level.getTile(adj).blocksMovement) continue; AStarNode node = new AStarNode(adj); if (closed.Contains(node)) continue; node.setParent(q); if (adj.Equals(end)) return node; node.g = q.g + 1; node.h = adj.distance(end); bool add = true; foreach (AStarNode openNode in open) { if (openNode.location.Equals(node.location) && openNode.f() < node.f()) add = false; } foreach (AStarNode closedNode in closed) { if (closedNode.location.Equals(node.location) && closedNode.f() < node.f()) add = false; } if (add) { open.Add(node); } } closed.Add(q); } return startNode; }
private static bool rectIsClear(Level level, Rect rect) { for (int x = rect.x1; x <= rect.x2; x++) { for (int y = rect.y1; y <= rect.y2; y++) { Tile tile = level.getTile(new Location(x, y)); if (tile == null || !tile.blocksMovement) return false; } } return true; }
private static bool isMonsterCandidate(Level level, Location location) { if (level.getEntities(location).Count > 1) return false; return !level.getTile(location).blocksMovement; }
private static bool isDoorCandidate(Level level, Location location) { if (!level.getTile(location).blocksMovement) return false; Location[] neighbors = { location.getAdjLocation(Direction.North), location.getAdjLocation(Direction.South), location.getAdjLocation(Direction.West), location.getAdjLocation(Direction.East) }; for (int i = 0; i < 4; i++) { Tile tile = level.getTile(neighbors[i]); if (tile != null && !tile.blocksMovement) return true; } return false; }
private static bool isDark(Level level, Location location) { if (level.getTile(location) == null || !level.getTile(location).blocksMovement) return false; Location[] neighbors = { location.getAdjLocation(Direction.North), location.getAdjLocation(Direction.South), location.getAdjLocation(Direction.West), location.getAdjLocation(Direction.East), location.getAdjLocation(Direction.NorthWest), location.getAdjLocation(Direction.SouthWest), location.getAdjLocation(Direction.NorthEast), location.getAdjLocation(Direction.SouthEast), }; int countBlockedNeighbors = 0; for (int i = 0; i < neighbors.Length; i++) { Tile tile = level.getTile(neighbors[i]); if (tile == null || tile.blocksMovement) { countBlockedNeighbors++; } } return countBlockedNeighbors == 8; }
private static bool isChestCandidate(Level level, Location location) { if (level.getTile(location).blocksMovement) return false; Direction[] directions = {Direction.North, Direction.South, Direction.West, Direction.East}; bool[] blocked = new bool[4]; for (int i = 0; i < directions.Length; i++) { Location adj = location.getAdjLocation(directions[i]); if (level.getWalkable(adj) is Door) return false; Tile tile = level.getTile(adj); if (tile != null && tile.blocksMovement) { blocked[i] = true; } } return !((blocked[0] && blocked[1]) || (blocked[2] && blocked[3])); }