diff options
Diffstat (limited to 'includes/maps.php')
-rw-r--r-- | includes/maps.php | 167 |
1 files changed, 87 insertions, 80 deletions
diff --git a/includes/maps.php b/includes/maps.php index 5ccb033..5be674e 100644 --- a/includes/maps.php +++ b/includes/maps.php @@ -363,92 +363,99 @@ function GenerateMap($width, $height, $rockchance, $numBlocks = -1, $cp = -1, $t if ($rockchance < 2) $rockchance = 2;
//== Possibility of inf loop here, if the map is too small.
- do {
- $randvalue = rand(1, ($width * $height));
- //As long as it isn't the first, or last column.
- //if ((($randvalue +1) % ($rows)) > 1) {
- //As long as it isn't in the first, 2nd, last and 2nd to last column.
- if ((($randvalue +2) % ($width)) > 3) {
- $unique[] = $randvalue;
- $unique = array_unique($unique);
- $unique = array_values($unique);
+ try {
+ $x = 0;
+ do {
+ $x++;
+ $randvalue = rand(1, ($width * $height));
+ //As long as it isn't the first, or last column.
+ //if ((($randvalue +1) % ($rows)) > 1) {
+ //As long as it isn't in the first, 2nd, last and 2nd to last column.
+ if ((($randvalue +2) % ($width)) > 3) {
+ $unique[] = $randvalue;
+ $unique = array_unique($unique);
+ $unique = array_values($unique);
+ }
+ } while (count($unique) < ($cp+$tp) AND $x < 10);
+
+ //Prepare our checkpoint and teleport names.
+ $cpnames = Array("a", "b", "c", "d", "e");
+ // in out in out etc.
+ $tpnames = Array("t", "u", "m", "n", 'g', 'h', 'i', 'j', 'k', 'l');
+
+ $teleport = Array();
+ $checkpoint = Array();
+ //Assign our checkpoints and teleports a unique position on the map.
+ $i = 0;
+ for($p = 0; $p < $cp; $p++) {
+ $checkpoint[$cpnames[$p]] = $unique[$i];
+ $i++;
+ }
+ for($p = 0; $p < $tp; $p++) {
+ $teleport[$tpnames[$p]] = $unique[$i];
+ $i++;
}
- } while (count($unique) < ($cp+$tp) );
-
- //Prepare our checkpoint and teleport names.
- $cpnames = Array("a", "b", "c", "d", "e");
- // in out in out etc.
- $tpnames = Array("t", "u", "m", "n", 'g', 'h', 'i', 'j', 'k', 'l');
-
- $teleport = Array();
- $checkpoint = Array();
- //Assign our checkpoints and teleports a unique position on the map.
- $i = 0;
- for($p = 0; $p < $cp; $p++) {
- $checkpoint[$cpnames[$p]] = $unique[$i];
- $i++;
- }
- for($p = 0; $p < $tp; $p++) {
- $teleport[$tpnames[$p]] = $unique[$i];
- $i++;
- }
- $rocks = 0; //Number of rocks in the maze.
+ $rocks = 0; //Number of rocks in the maze.
- // We need to make sure the map we construct is valid.
- // so we throw this in a do-while the map is invalid.
- do {
- $p = -1;
- //Begin loop to populate grid.
- for( $y = 1; $y <= $height; $y++) { //Number of Rows
- for( $x = 0; $x < $width; $x++) { //Number of Columns
- $p++;
- //Start and Finish squares.
- if ($x == 0) {
- //if ($x == 0 AND $y == 1) {
- //if ($x == 0 AND rand(1,3) == 1) {
- $grid[$y][$x] = "s";
- } elseif ($x == $width - 1) {
- //} elseif ($x == $rows - 1 AND $y == intval(($cols + 1) * .5) ) {
- //} elseif ($x == $rows - 1 AND rand(1,3) == 1) {
- $grid[$y][$x] = "f";
- //Randomly Placed Rocks
- } elseif (rand(1, $rockchance) == 2) {
- $grid[$y][$x] = "r";
- $rocks++;
- //TODO: rock count could be off if covered by checkpoint.
- //Just a normal square.
- } else {
- $grid[$y][$x] = "o";
- }
- //Absolutely placed points; Checkpoints.
- foreach ($checkpoint as $key => $v) {
- if ($v == $p) {
- $grid[$y][$x] = $key;
+ // We need to make sure the map we construct is valid.
+ // so we throw this in a do-while the map is invalid.
+ $x = 0;
+ do {
+ $x++;
+ $p = -1;
+ //Begin loop to populate grid.
+ for( $y = 1; $y <= $height; $y++) { //Number of Rows
+ for( $x = 0; $x < $width; $x++) { //Number of Columns
+ $p++;
+ //Start and Finish squares.
+ if ($x == 0) {
+ //if ($x == 0 AND $y == 1) {
+ //if ($x == 0 AND rand(1,3) == 1) {
+ $grid[$y][$x] = "s";
+ } elseif ($x == $width - 1) {
+ //} elseif ($x == $rows - 1 AND $y == intval(($cols + 1) * .5) ) {
+ //} elseif ($x == $rows - 1 AND rand(1,3) == 1) {
+ $grid[$y][$x] = "f";
+ //Randomly Placed Rocks
+ } elseif (rand(1, $rockchance) == 2) {
+ $grid[$y][$x] = "r";
+ $rocks++;
+ //TODO: rock count could be off if covered by checkpoint.
+ //Just a normal square.
+ } else {
+ $grid[$y][$x] = "o";
}
- } //Teleports too
- foreach ($teleport as $key => $v) {
- if ($v == $p) {
- $grid[$y][$x] = $key;
+ //Absolutely placed points; Checkpoints.
+ foreach ($checkpoint as $key => $v) {
+ if ($v == $p) {
+ $grid[$y][$x] = $key;
+ }
+ } //Teleports too
+ foreach ($teleport as $key => $v) {
+ if ($v == $p) {
+ $grid[$y][$x] = $key;
+ }
}
- }
-
- } //Rows
- } //Cols
- //Fill $grid[0] with header information
- $grid[0][0] = $width;
- $grid[0][1] = $height;
- $grid[0][2] = count($checkpoint);
- $grid[0][3] = $rocks;
- $grid[0][4] = $numBlocks;
- $grid[0][5] = count($teleport);
- $grid[0][6] = $mapName;
-
- //Validate map
- $path = routePath($grid, true);
- //Only repeat if it's blocked.
- } while ($path['blocked'] == true);
+ } //Rows
+ } //Cols
+ //Fill $grid[0] with header information
+ $grid[0][0] = $width;
+ $grid[0][1] = $height;
+ $grid[0][2] = count($checkpoint);
+ $grid[0][3] = $rocks;
+ $grid[0][4] = $numBlocks;
+ $grid[0][5] = count($teleport);
+ $grid[0][6] = $mapName;
+
+ //Validate map
+ $path = routePath($grid, true);
+ //Only repeat if it's blocked. - And if it's tried less than 200 times.
+ } while ($path['blocked'] == true AND $x < 10);
+ } catch (Exception $e) {
+ echo 'Caught exception: ', $e->getMessage(), "\n";
+ }
return $grid;
}
|