void commitColorChangeToGrid(IEnumerable <Vector3> list, Color c) { if (!colorDict.ContainsKey(c)) { colorDict[c] = new List <GridUnit>(); } foreach (Vector3 v in list) { if (grid.ContainsKey(v)) { colorDict[c].Remove(grid[v]); grid[v].changeColor(c); } else { grid[v] = new GridUnit(v, c); } colorDict[c].Add(grid[v]); if (c == Color.white) { foreach (Vector3 v0 in VoxelIterators.FullNeighbors()) { var v2 = v0 + v; if (!list.Contains(v2) && grid.ContainsKey(v2) && grid[v2].C != Color.white) { grid[v2].value += 1; } } } } }
public void removeNeighbors(List <Cell> cells, Cell c) { cells.Remove(c); foreach (Vector3 v in VoxelIterators.VonNeumanNeighbors()) { var c2 = cells.Find(x => x.center == v + c.center); if (c2 != null && !c2.occupied) { removeNeighbors(cells, c2); } } }
void recurse(Vector3 v, HashSet <Vector3> group) { if (!group.Contains(v)) { return; } group.Remove(v); foreach (Vector3 v0 in VoxelIterators.VonNeumanNeighbors()) { recurse(v0 + v, group); } }
void allocateWhiteSpace(List <Vector3> black, List <Vector3> white) { foreach (Vector3 v in black) { foreach (Vector2 v0 in VoxelIterators.FullNeighbors()) { var v2 = v + (Vector3)v0; if (grid.ContainsKey(v2) && grid[v2].C != Color.black) { white.Add(v2); } } } }
public void createBorderGeometry() { foreach (Cell cell in cells.Values) { var v = cell.center; foreach (Vector3 v2 in VoxelIterators.VonNeumanNeighbors3D()) { if (cells.ContainsKey(v2 + v)) { cell.neighbors.Add(v2 + v); cell.removeWall(cells[v2 + v]); cells[v2 + v].removeWall(cell); } } } }
void fill(Room room, PropUnitCollection propUnitCollection, GameObject parent) { var points = propUnitCollection.getListOfPoints(); var neighbors = new List <Vector3>(); foreach (Vector3 v in points) { var cell = room.cells[v]; var walls = cell.findWallWithAttribute(FaceTypes.WALL); foreach (Shape s in walls) { neighbors.Add(s.normal.normalized); } foreach (Vector3 v0 in VoxelIterators.VonNeumanNeighbors()) { var v2 = v + v0; if (grid.ContainsKey(v2) && grid[v2].C == Color.white) { neighbors.Add(v0); } } } Vector3 horizontal; if (neighbors.Count == 0) { horizontal = Vector3.right; } else { var grouping = neighbors.GroupBy(a => a.GetHashCode()); var groupList = grouping.OrderByDescending(b => b.Count()); horizontal = groupList.First().First(); } foreach (PropUnit propUnit in propUnitCollection.list) { var center = propUnit.b.center; var vertical = Vector3.forward; var prop = GameObject.Instantiate(propUnit.overlay.prop); Aligner.AlignBoundingBoxFaceToBoundingBoxFace(horizontal, vertical, propUnit.b, prop); prop.transform.parent = parent.transform; } }
public static void recurse(Vector3 v, HashSet <Vector3> points, HashSet <Vector3> visitedPoints) { if (visitedPoints.Contains(v)) { return; } visitedPoints.Add(v); points.Remove(v); foreach (Vector3 v2 in VoxelIterators.VonNeumanNeighbors3D()) { var v3 = v2 + v; if (points.Contains(v3)) { recurse(v3, points, visitedPoints); } } }
public static Dictionary <Cell, Cell> touchingCells(Room a, Room b) { Dictionary <Cell, Cell> d = new Dictionary <Cell, Cell>(); foreach (Vector3 v in a.pointsInRoom) { foreach (Vector3 v2 in VoxelIterators.VonNeumanNeighbors3D()) { var v3 = v + v2; if (b.pointsInRoom.Contains(v3)) { var c1 = a.cells[v]; var c2 = b.cells[v3]; d[a.cells[v]] = b.cells[v3]; } } } return(d); }
public static List <Shape> WallUnion(Room a, Room b) { var walls = new List <Shape>(); foreach (Vector3 v in a.pointsInRoom) { foreach (Vector3 v2 in VoxelIterators.VonNeumanNeighbors3D()) { var v3 = v + v2; if (b.pointsInRoom.Contains(v3)) { var c = a.cells[v].walls; var d = b.cells[v3].walls; walls.AddRange(c.Intersect(d)); } } } return(walls); }
/*public bool checkHorizontalDirection (List<Cell> output, List<Vector3> vertical, List<Vector3> graph, Vector3 direction, int height,Staircase stairCase){ * var crawler = new Crawler(); * var obstructingCells = new List<Cell>(); * for (int i = 0; i < vertical.Count;i++){ * * var c = vertical[i]; * * var p = c.center; * * var horizontalPoints = crawler.crawl(direction,p,graph,height + 2); * * * if (horizontalPoints.Count != height + 2){ * return false; * } * output.AddRange(horizontalPoints); * obstructingCells.AddRange(horizontalPoints); * } * * stairCase.direction = direction; * stairCase.minimum = vertical[0]; * stairCase.start = graph.cells[vertical[0].center + Vector3.forward * height]; * stairCase.end = graph.cells[vertical[0].center + direction * stairCase.distance]; * * * bool pass1 = false; * bool pass2 = false; * foreach (Vector3 v in VoxelIterators.VonNeumanNeighbors()){ * var v1 = v + stairCase.start.center; * var v2 = v + stairCase.end.center; * if (graph.cells.ContainsKey(v1) && ! graph.cells[v1].occupied){ * pass1 = true; * } * if (graph.cells.ContainsKey(v2) && ! graph.cells[v2].occupied){ * pass2 = true; * } * } * if (! pass1 || !pass2) return false; * * stairCase.distance = height + 2; * stairCase.height = height; * return checkFloorConnectivity(graph.cells.Values,obstructingCells,2); * }*/ /*public Room constructStairCase (Staircase staircase, CellMap graph,List<Cell> output){ * var direction = staircase.direction; * var min = staircase.minimum.center; * var cellsToRemove = new List<Cell>(); * var height = staircase.height; * * Cell start = null; * Cell end = null; * Vector3 center; * Bounds bounds; * * for (int h = 0; h < height;h++){ * for (int d = 0; d < staircase.distance;d++){ * * center = min + h * Vector3.forward + d * staircase.direction; * * GameObject g = null; * bounds = new Bounds(center,Vector3.one); * * if (d == 0){ * * if (h == height - 1) start = graph.cells[center + Vector3.forward]; * * g = GameObject.CreatePrimitive(PrimitiveType.Cube); * * * } else if (d < staircase.distance - 1){ * if (d + h == height){ * g = GameObject.Instantiate(stairCasePiece); * * } else if (d + h < height){ * g = GameObject.CreatePrimitive(PrimitiveType.Cube); * * } * * * * } else { * if (h == 0) { * end = graph.cells[center]; * g = GameObject.Instantiate(railingEnd); * } * * } * if (g != null) Aligner.AlignBoundingBoxFaceToBoundingBoxFace(direction,Vector3.forward,bounds,g); * * * if (h >= height - 2 && h > 0 && d > 0){ * center += Vector3.forward; * cellsToRemove.Add(graph.cells[center]); * } * } * } * * var temp = GameObject.Instantiate(railingStart); * center = min + height * Vector3.forward; * bounds = new Bounds(center,Vector3.one); * Aligner.AlignBoundingBoxFaceToBoundingBoxFace(direction,Vector3.forward,bounds,temp); * * cellsToRemove.Remove(start); * cellsToRemove.Remove(end); * * * return markCellsAsOccupied(output,previous,currentRoom,cellsToRemove,start,end); * }*/ /*Room markCellsAsOccupied (List<Cell> output,Room r1, Room r2,List<Cell> cellsToRemove, Cell start, Cell end) { * var room = new Room(output); * * foreach (Vector3 v in room.pointsInRoom){ * r1.removeCell(v); * r2.removeCell(v); * } * room.refresh(); * room.createBorderGeometry(); * * foreach (Vector3 v in room.cells.Keys){ * var c = room.cells[v]; * if (c.center.Equals(start.center)){ * c.occupied = false; * start= c; * } else if (c.center.Equals(end.center)){ * end = c; * c.occupied = false; * } else { * c.occupied = true; * } * } * start.restoreFloor(); * end.restoreFloor(); * room.tag = new BoundedSpaceTags(FaceTypes.STAIRCASE); * return room; * * } */ public Room findSuitableStaircaseLocation(int height, Vector3 previousLocation, HashSet <Vector3> graph) { var crawler = new Crawler(); var points = graph.OrderBy(x => (x - previousLocation).magnitude).ToList(); //Debug.Log("Graph count: " + graph.Count); var c = 0; while (points.Count > 0) { var p = points[0]; points.RemoveAt(0); var vertPoints = crawler.crawl(Vector3.forward, p, graph, 4); var downPoints = crawler.crawl(Vector3.back, p, graph, 1); vertPoints = vertPoints.OrderBy(x => x.z).ToList(); if (vertPoints.Count == 4 && downPoints.Count == 1) { foreach (Vector3 direction in VoxelIterators.VonNeumanNeighbors()) { var horizontalPoints = new List <Vector3>(); bool badLocation = false; foreach (Vector3 v in vertPoints) { var line = crawler.crawl(direction, v, graph, 4); if (line.Count != 4) { badLocation = true; } else { horizontalPoints.AddRange(line); } } if (!badLocation) { return(new Room(horizontalPoints)); } } } } return(null); }
public static EdgeMap constructEdgemapFromPoints(IEnumerable <Vector3> input) { var edgeMap = new EdgeMap(); var edges = new HashSet <Edge>(); foreach (Vector3 v in input) { foreach (Vector3 v2 in VoxelIterators.VonNeumanNeighbors3D()) { var v3 = v + v2; if (input.Contains(v3)) { edges.Add(new Edge(v, v3)); } } } foreach (Edge e in edges) { edgeMap.addElement(e); } return(edgeMap); }
public List <PropUnitCollection> partitionSpace(Room room) { var rects = new Dictionary <Rect, PropOverlay>(); var POG = propOverlays[0].propOverlays; var propQueue = new Queue <PropOverlay>(POG); var output = new List <PropUnitCollection>(); var list = getListOfPointsOfColor(Color.grey); list = list.OrderByDescending(x => x.value).ToList(); var queue = new Queue <GridUnit>(list); for (int q = 0; q < cutoff; q++) { if (list.Count == 0) { break; } var gU = queue.Dequeue(); var p = gU.V; queue.Enqueue(gU); PropOverlay overlay = propQueue.Peek(); if (!overlay.highPriority) { propQueue.Dequeue(); propQueue.Enqueue(overlay); } int iter = 1; if (overlay.extendable) { iter = r.Next(3, 6); } List <PropUnitCollection> possibilities = new List <PropUnitCollection>(); foreach (Vector3 direction in VoxelIterators.VonNeumanNeighbors()) { var d2 = new Vector3(direction.x * overlay.size.x, direction.y * overlay.size.y, direction.z * overlay.size.z); var rot = Quaternion.identity; var black = new PropUnitCollection(); if (recurse(iter, d2, p, rot, overlay, room, black) && black.list.Count > 0) { possibilities.Add(black); } } possibilities = possibilities.OrderByDescending(x => x.list.Count).ToList(); if (possibilities.Count > 0) { var first = possibilities.First(); var black = first.getListOfPoints(); if (black.Count > 0) { output.Add(first); commitColorChangeToGrid(black, Color.black); var whitespace = new List <Vector3>(); allocateWhiteSpace(black, whitespace); commitColorChangeToGrid(whitespace, Color.white); list = getListOfPointsOfColor(Color.grey); list = list.OrderByDescending(x => x.value).ToList(); queue = new Queue <GridUnit>(list); } if (overlay.highPriority) { propQueue.Dequeue(); } } } //foreach (GridUnit v in getListOfPointsOfColor(Color.white)) GameObject.Instantiate(g1).transform.position = v.V; //foreach (GridUnit v in getListOfPointsOfColor(Color.black)) GameObject.Instantiate(g2).transform.position = v.V; return(output); }