From 5c76c4160bff16cdd3494736033b427e76d76e73 Mon Sep 17 00:00:00 2001 From: Patrick Davison Date: Sat, 16 Apr 2011 12:54:41 -0700 Subject: 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. --- includes/maps.php | 138 +++++++++++++++++++++++++++--------------------------- 1 file changed, 69 insertions(+), 69 deletions(-) (limited to 'includes') 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 .= ""; break; case 'r': $maptable .= ""; break; //rock + case 'q': $maptable .= ""; break; //rock case 'w': $maptable .= ""; break; //wall //default: $maptable .= "".$index.""; default: $maptable .= ""; @@ -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 .= " +
+ + + + + + +
"; + $mapdatadiv .= "'; - $maptable = " + $maptable = "$preloaddiv +
$maptable
"; @@ -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 "
".$index.$mapMatrix[$i][$j]."
"; } $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 "
Map Parameters:
"; echo "mapMatrix[0][0]: ".$mapMatrix[0][0]."
"; echo "mapMatrix[0][1]: ".$mapMatrix[0][1]."
"; @@ -346,7 +370,7 @@ function GenerateMapByCode($code) { echo "mapMatrix[0][3]: ".$mapMatrix[0][3]."
"; echo "mapMatrix[0][4]: ".$mapMatrix[0][4]."
"; echo "mapMatrix[0][5]: ".$mapMatrix[0][5]."
"; - */ + */ //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."
"; - //echo "number:".$next."
"; - //echo "original:".$splitCode[$index]."
"; } else { $mapMatrix[$i][$j] = 'o'; //Empty Tile } - //echo "Value:".$mapMatrix[$i][$j]."
"; } } - - //echo "mapMatrix[1][0]: ".$mapMatrix[1][0]."
"; - //echo "mapMatrix[1][1]: ".$mapMatrix[1][1]."
"; - 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 "
"; - - $index += (int)$splitCode[$i]; - $type = $splitCode[$i][strlen($splitCode[$i])-1]; - - //$tile = GetTile($mapMatrix, $index); - echo "$x = (int)($index / ".$mapMatrix[0][0].");
"; - $x = (int)($index / $mapMatrix[0][0]); - $y = $id % $mapMatrix[0][0]; - - - echo "splitCode: ".$splitCode[$i]."
"; - echo "Index: ".$index." -- ".$x.",".$y." to ".$type; - echo "
"; - - $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. -- cgit v1.2.3