void Connect(BSPTree bsp, int i, bool isLeft, int z) { if (bsp.IsLeaf(i)) return; var left = bsp.GetLeft(i); var right = bsp.GetRight(i); Connect(bsp, left, true, z); Connect(bsp, right, false, z); var leftRoom = FindNearestRoom(bsp, left, bsp[right].Grid.Center); var rightRoom = FindNearestRoom(bsp, right, bsp[left].Grid.Center); ConnectRooms(bsp[leftRoom].Grid, bsp[rightRoom].Grid, z); }
void Connect(BSPTree bsp, int i, bool isLeft, int z) { if (bsp.IsLeaf(i)) { return; } var left = bsp.GetLeft(i); var right = bsp.GetRight(i); Connect(bsp, left, true, z); Connect(bsp, right, false, z); var leftRoom = FindNearestRoom(bsp, left, bsp[right].Grid.Center); var rightRoom = FindNearestRoom(bsp, right, bsp[left].Grid.Center); ConnectRooms(bsp[leftRoom].Grid, bsp[rightRoom].Grid, z); }
int FindNearestRoom(BSPTree bsp, int i, IntVector2 p) { if (bsp.IsLeaf(i)) { return(i); } else { int left = FindNearestRoom(bsp, bsp.GetLeft(i), p); int right = FindNearestRoom(bsp, bsp.GetRight(i), p); double dl = (bsp[left].Grid.Center - p).Length; double rl = (bsp[right].Grid.Center - p).Length; if (dl < rl) { return(left); } else { return(right); } } }
int FindNearestRoom(BSPTree bsp, int i, IntVector2 p) { if (bsp.IsLeaf(i)) { return i; } else { int left = FindNearestRoom(bsp, bsp.GetLeft(i), p); int right = FindNearestRoom(bsp, bsp.GetRight(i), p); double dl = (bsp[left].Grid.Center - p).Length; double rl = (bsp[right].Grid.Center - p).Length; if (dl < rl) return left; else return right; } }
void CreateNodes(BSPTree bsp, IntGrid2 grid, int i) { int middle; bool horiz; if (grid.Columns <= 4 && grid.Rows <= 4) { Debugger.Break(); throw new Exception(); } else if (grid.Columns <= 4) { horiz = true; } else if (grid.Rows <= 4) { horiz = false; } else { horiz = grid.Columns < grid.Rows; } double m = GetRandomDouble(0.4, 0.6); if (horiz) middle = (int)(grid.Rows * m); else middle = (int)(grid.Columns * m); bsp[i] = new BSPNode(grid, horiz); if (bsp.IsLeaf(i)) return; int left = bsp.GetLeft(i); int right = bsp.GetRight(i); if (horiz) { // up var g1 = new IntGrid2(grid.X, grid.Y, grid.Columns, middle); CreateNodes(bsp, g1, left); // down var g2 = new IntGrid2(grid.X, grid.Y + middle + 1, grid.Columns, grid.Rows - middle - 1); CreateNodes(bsp, g2, right); } else { // left var g1 = new IntGrid2(grid.X, grid.Y, middle, grid.Rows); CreateNodes(bsp, g1, left); // right var g2 = new IntGrid2(grid.X + middle + 1, grid.Y, grid.Columns - middle - 1, grid.Rows); CreateNodes(bsp, g2, right); } }
void CreateNodes(BSPTree bsp, IntGrid2 grid, int i) { int middle; bool horiz; if (grid.Columns <= 4 && grid.Rows <= 4) { Debugger.Break(); throw new Exception(); } else if (grid.Columns <= 4) { horiz = true; } else if (grid.Rows <= 4) { horiz = false; } else { horiz = grid.Columns < grid.Rows; } double m = GetRandomDouble(0.4, 0.6); if (horiz) { middle = (int)(grid.Rows * m); } else { middle = (int)(grid.Columns * m); } bsp[i] = new BSPNode(grid, horiz); if (bsp.IsLeaf(i)) { return; } int left = bsp.GetLeft(i); int right = bsp.GetRight(i); if (horiz) { // up var g1 = new IntGrid2(grid.X, grid.Y, grid.Columns, middle); CreateNodes(bsp, g1, left); // down var g2 = new IntGrid2(grid.X, grid.Y + middle + 1, grid.Columns, grid.Rows - middle - 1); CreateNodes(bsp, g2, right); } else { // left var g1 = new IntGrid2(grid.X, grid.Y, middle, grid.Rows); CreateNodes(bsp, g1, left); // right var g2 = new IntGrid2(grid.X + middle + 1, grid.Y, grid.Columns - middle - 1, grid.Rows); CreateNodes(bsp, g2, right); } }