diff options
author | Patrick Davison <snapwilliam@gmail.com> | 2011-04-16 12:54:41 -0700 |
---|---|---|
committer | Patrick Davison <snapwilliam@gmail.com> | 2011-04-16 12:54:41 -0700 |
commit | 5c76c4160bff16cdd3494736033b427e76d76e73 (patch) | |
tree | f4d9c5e45792d62cccfb9336b450aaae610b38ab /includes/maps.php | |
parent | 6a834c3db575118a20da1276e372a5edf66ac81d (diff) | |
download | pathery-5c76c4160bff16cdd3494736033b427e76d76e73.tar.xz |
Findtiles function added.
Now used to; locate start locations and teleport out's.
Fixes involving the use of randomly placed start locations.
Added tile "q" for blank spaces - for use in oddly shaped maps.
Diffstat (limited to 'includes/maps.php')
-rw-r--r-- | includes/maps.php | 138 |
1 files changed, 69 insertions, 69 deletions
diff --git a/includes/maps.php b/includes/maps.php index acb4c6c..04bf375 100644 --- a/includes/maps.php +++ b/includes/maps.php @@ -50,6 +50,7 @@ function DisplayMap($mapMatrix, $idprefix = 1, $example = false, $speed = NULL) case 'e': $maptable .= "<td class='grid_td_cpe' id='$handle' ></td>"; break;
case 'r': $maptable .= "<td class='grid_td_rocks' id='$handle' ></td>"; break; //rock
+ case 'q': $maptable .= "<td class='grid_td_blanks' id='$handle' ></td>"; break; //rock
case 'w': $maptable .= "<td class='grid_td_walls' id='$handle' name='true' onClick='grid_click(this)' ></td>"; break; //wall
//default: $maptable .= "<td class='grid_td' id='$handle' onClick='grid_click(this)' >".$index."</td>";
default: $maptable .= "<td title='Position: $i,$j' class='grid_td' id='$handle' onClick='grid_click(this)' ></td>";
@@ -78,11 +79,27 @@ function DisplayMap($mapMatrix, $idprefix = 1, $example = false, $speed = NULL) //$height = (($i * 22) + 2).'px';
$jsonmap = json_encode($mapdata);
+
+
+
+
+ //This works in chrome, not sure about others.
+ $preloaddiv .= "
+ <div style='visibility:hidden;display:none'>
+ <img src='images/Path1.png' />
+ <img src='images/Path2.png' />
+ <img src='images/Path3.png' />
+ <img src='images/Path4.png' />
+ <img src='images/OverlayTileFaceted30.png' />
+ <img src='images/OverlayTileFaceted10.png' />
+ </div>";
+
$mapdatadiv .= "<div id='$idprefix,mapdata' style='visibility:hidden;display:none'>";
$mapdatadiv .= $jsonmap;
$mapdatadiv .= '</div>';
- $maptable = "<table style='width:$width;height:$height;' class='grid_table'>
+ $maptable = "$preloaddiv
+ <table style='width:$width;height:$height;' class='grid_table'>
$maptable
</table>";
@@ -177,7 +194,16 @@ function DisplayMap($mapMatrix, $idprefix = 1, $example = false, $speed = NULL) //Generates map
function GenerateMap($rows, $cols, $rockchance, $numBlocks = -1, $cp = -1, $tp = -1) {
- //!! Possibility of inf loop here.
+ if ($numBlocks == -1)
+ $numBlocks = Rand(7, (int)($rows * $cols) * .12);
+ //Checkpoints and teleports.
+ if ($cp == -1)
+ $cp = rand(0, 5);
+ if ($tp == -1)
+ $tp = rand(0, 2);
+ $tp = $tp * 2; //Requires an out-teleport.
+
+ //== Possibility of inf loop here, if the map is too small.
do {
$randvalue = rand(1, ($rows * $cols));
//As long as it isn't the first, or last column.
@@ -188,23 +214,16 @@ function GenerateMap($rows, $cols, $rockchance, $numBlocks = -1, $cp = -1, $tp = $unique = array_unique($unique);
$unique = array_values($unique);
}
- } while (count($unique) < 15);
-
- if ($numBlocks == -1)
- $numBlocks = Rand(7, (int)($rows * $cols) * .12);
+ } while (count($unique) < ($cp+$tp) );
- if ($cp == -1)
- $cp = rand(0, 5);
- if ($tp == -1)
- $tp = rand(0, 2);
- $tp = $tp * 2; //Requires an out-teleport.
-
+ //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];
@@ -218,7 +237,7 @@ function GenerateMap($rows, $cols, $rockchance, $numBlocks = -1, $cp = -1, $tp = $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.
+ // so we throw this in a do-while the map is invalid.
do {
$p = -1;
//Begin loop to populate grid.
@@ -227,10 +246,14 @@ function GenerateMap($rows, $cols, $rockchance, $numBlocks = -1, $cp = -1, $tp = $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 == $rows - 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
+ //Randomly Placed Rocks
} elseif (rand(1, $rockchance) == 2) {
$grid[$y][$x] = "r";
$rocks++;
@@ -275,9 +298,6 @@ function GenerateMapCode($mapMatrix) { //Iterate through $mapMatrix and generate the code used to save and
// load the map through the database.
- // 0.1 Snap. (added mapsize header data)
- // 0.2 Rex - Added #checkpoints, #rocks, #walls to header data; adjusted loops.
-
$code = ""; //The string to return to the database.
$index = 0; //The current number of tiles from the last tile saved.
@@ -304,12 +324,16 @@ function GenerateMapCode($mapMatrix) { {
//As long as the tile is NOT open, embed it in the code.
$code .= $index.$mapMatrix[$i][$j].'.';
+ //Start from 0 again.
$index = -1;
- //==echo "<br /><b>".$index.$mapMatrix[$i][$j]."</b><br />";
}
$index += 1;
}
}
+ //== Don't need to fill in the last spot.
+ //if ($index > 1) {
+ // $code .= $index.'o.';
+ //}
return $code;
}
@@ -338,7 +362,7 @@ function GenerateMapByCode($code) { $mapMatrix[0][5] = (int)substr($headers[4], 1); //Number of Teleports
//Printing out parameters for debug purposes...
- /*
+ /*
echo "<br />Map Parameters:<br />";
echo "mapMatrix[0][0]: ".$mapMatrix[0][0]."<br />";
echo "mapMatrix[0][1]: ".$mapMatrix[0][1]."<br />";
@@ -346,7 +370,7 @@ function GenerateMapByCode($code) { echo "mapMatrix[0][3]: ".$mapMatrix[0][3]."<br />";
echo "mapMatrix[0][4]: ".$mapMatrix[0][4]."<br />";
echo "mapMatrix[0][5]: ".$mapMatrix[0][5]."<br />";
- */
+ */
//Begin creating our mapMatrix
$t = -1;
$index = 0;
@@ -355,54 +379,24 @@ function GenerateMapByCode($code) { $t++;
$next = substr($splitCode[$index], 0, strlen($splitCode[$index]) - 1);
- if ($next == $t) {
+ //Are we at the next target, if there is one.
+ if ($next == $t AND $next != '') {
+ //Update tile.
$type = substr($splitCode[$index], -1, 1);
$mapMatrix[$i][$j] = $type;
$index++;
+ //Start from 0 again.
$t = -1;
- //echo "type:".$type."<br />";
- //echo "number:".$next."<br />";
- //echo "original:".$splitCode[$index]."<br />";
} else {
$mapMatrix[$i][$j] = 'o'; //Empty Tile
}
- //echo "Value:".$mapMatrix[$i][$j]."<br />";
}
}
-
- //echo "mapMatrix[1][0]: ".$mapMatrix[1][0]."<br />";
- //echo "mapMatrix[1][1]: ".$mapMatrix[1][1]."<br />";
-
return $mapMatrix;
- //Snap Stops
-
- //Iterate through the code and adjust spaces as directed.
- /* UNREACHABLE CODE
- $index = 0;
- for ( $i = 4; $i < count($splitCode); $i++)
- {
- echo "<br />";
-
- $index += (int)$splitCode[$i];
- $type = $splitCode[$i][strlen($splitCode[$i])-1];
-
- //$tile = GetTile($mapMatrix, $index);
- echo "$x = (int)($index / ".$mapMatrix[0][0].");<br />";
- $x = (int)($index / $mapMatrix[0][0]);
- $y = $id % $mapMatrix[0][0];
-
-
- echo "splitCode: ".$splitCode[$i]."<br />";
- echo "Index: ".$index." -- ".$x.",".$y." to ".$type;
- echo "<br />";
-
- $mapMatrix[$x][$y] = $type;
- }
-
- return $mapMatrix; */
}
+
//Returns a mapMatrix merged with a solution/maze.
function MergeMapSolution($mapMatrix, $solution) {
//echo $solution;
@@ -489,8 +483,15 @@ function pastMap($maptype, $daysago) { return $r;
}
-
-
+function findTiles ($mapMatrix, $search) {
+ for( $i = 1; $i <= $mapMatrix[0][1]; $i++) { //Number of Rows
+ for( $j = 0; $j < $mapMatrix[0][0]; $j++) { //Number of Columns
+ if ($mapMatrix[$i][$j] == $search)
+ $r.= "$j,$i.";
+ }
+ }
+ return $r;
+}
// Returns: ARRAY( blocked, path, start, end )
@@ -588,11 +589,12 @@ function GetTile($mapMatrix, $id) function routePath($mygrid, $start = '') {
//== This should grab the start positions by scaning the map.
- if ($start == '') {
- for ($i = 1; $i <= $mygrid[0][1]; $i++) {
- $start .= "0,$i.";
- }
- }
+ //if ($start == '') {
+ // for ($i = 1; $i <= $mygrid[0][1]; $i++) {
+ // $start .= "0,$i.";
+ // }
+ //}
+ $start = findTiles($mygrid, "s");
//Checkpoint names
$cpnames = Array("a", "b", "c", "d", "e");
@@ -637,9 +639,6 @@ function routePath($mygrid, $start = '') { if ($r['start'] == '')
$r['start'] = $p['start'];
- //$f = 0;
- //1449
- //2263
do {
//Make sure there is a teleport to search.
if (! is_array($teleport))
@@ -672,10 +671,11 @@ function routePath($mygrid, $start = '') { $outchar = $teleout[$port];
if ($position === false) continue;
- //if ($teleactive[$port] === false) continue;
//Where is the tele $out location.
- $x = Findpath($mygrid, $start, $outchar);
- $out = $x['end'];
+ // == String/array, messy.. needs a bit of changes to fix though.
+ $out = findTiles($mygrid, $outchar);
+ $out = explode(".", $out);
+ $out = $out[0];
if ($x['blocked']) $blocked = true; //Optional?
//New path starting from our out-location.
|