/** * Locate a square in y1 <= y < y2, x1 <= x < x2 which satisfies the given * predicate. */ static void cave_find_in_range(Cave c, out int y, int y1, int y2, out int x, int x1, int x2, cave_predicate_func pred) { int yd = y2 - y1; int xd = x2 - x1; int i, n = yd * xd; /* Allocate the squares, and randomize their order */ int[] squares = new int[n]; for (i = 0; i < n; i++) squares[i] = i; //This doesn't look random to me... /* Do the actual search */ _find_in_range(c, out y, y1, y2, out x, x1, x2, squares, pred); /* Deallocate memory, make sure we found an empty square, and return */ //FREE(squares); }
/** * Locate a square in y1 <= y < y2, x1 <= x < x2 which satisfies the given * predicate. */ static void _find_in_range(Cave c, out int y, int y1, int y2, out int x, int x1, int x2, int[] squares, cave_predicate_func pred) { int yd = y2 - y1; int xd = x2 - x1; int i, n = yd * xd; bool done = false; x = 0; y = 0; /* Test each square in (random) order for openness */ for (i = 0; i < n && !done; i++) { int j = Random.randint0(n - i) + i; int k = squares[j]; squares[j] = squares[i]; squares[i] = k; y = (k / xd) + y1; x = (k % xd) + x1; if (pred(c, y, x)) done = true; } /* Deallocate memory, make sure we found an empty square, and return */ Misc.assert(done); }
/** * Locate a square in the dungeon which satisfies the given predicate. */ static void cave_find(Cave c, out int y, out int x, cave_predicate_func pred) { _find_in_range(c, out y, 0, c.height, out x, 0, c.width, cave_squares, pred); }