diff options
Diffstat (limited to 'includes/maps.php')
-rw-r--r-- | includes/maps.php | 434 |
1 files changed, 310 insertions, 124 deletions
diff --git a/includes/maps.php b/includes/maps.php index acb4c6c..7be04eb 100644 --- a/includes/maps.php +++ b/includes/maps.php @@ -1,4 +1,5 @@ <?PHP
+//Displays the map
function DisplayMap($mapMatrix, $idprefix = 1, $example = false, $speed = NULL) {
//Iterate through $mapMatrix and generate the html
$maptable = ""; //The string to return to the database.
@@ -50,6 +51,11 @@ 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 'R': $maptable .= "<td class='grid_td_gray' id='$handle' ></td>"; break; //metalic looking rock
+
+ case 'p': $maptable .= "<td class='grid_td_path' id='$handle' ></td>"; break; //path.
+ case 'q': $maptable .= "<td class='grid_td_blanks' id='$handle' ></td>"; break; //transparent
+
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 +84,28 @@ 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/OverlayTileFaceted50.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>";
@@ -174,10 +197,20 @@ function DisplayMap($mapMatrix, $idprefix = 1, $example = false, $speed = NULL) return $output;
}
+
//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 +221,16 @@ function GenerateMap($rows, $cols, $rockchance, $numBlocks = -1, $cp = -1, $tp = $unique = array_unique($unique);
$unique = array_values($unique);
}
- } while (count($unique) < 15);
+ } while (count($unique) < ($cp+$tp) );
- if ($numBlocks == -1)
- $numBlocks = Rand(7, (int)($rows * $cols) * .12);
-
- 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 +244,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 +253,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++;
@@ -261,23 +291,194 @@ function GenerateMap($rows, $cols, $rockchance, $numBlocks = -1, $cp = -1, $tp = $grid[0][4] = $numBlocks;
$grid[0][5] = count($teleport);
- //Confirm the map isn't broken to start-out.
- $path = routePath($grid);
+ //Validate map
+ $path = routePath($grid, '', true);
//Only repeat if it's blocked.
} while ($path['blocked'] == true);
return $grid;
}
+//Generates map based on $shape.
+function GenerateShapedMap($shape, $params) {
+
+ //Get width and height.
+ $cols = strlen($shape[0]);
+ $rows = count($shape);
+
+ //Scan for checkpoints.
+ //$checkpoints = 0;
+ //$cpnames = Array("a", "b", "c", "d", "e");
+ //Get the amount of checkpoints on this map.
+ //foreach ($cpnames as $cpt)
+ // if (findTiles($mygrid, $cpt))
+ // $checkpoints++;
+
+ //Confirm params.
+ if ($params['rockchance'])
+ $rockchance = $params['rockchance'];
+ else
+ $rockchance = 10;
+
+ if ($params['checkpoints'])
+ $checkpoints = $params['checkpoints'];
+ else
+ $checkpoints = 0;
+
+ if ($params['teleports'])
+ $teleports = $params['teleports'];
+ else
+ $teleports = 0;
+
+ if ($params['walls'])
+ $walls = $params['walls'];
+ else
+ $walls = 13;
+
+ $mapMatrix[0][0] = $cols;
+ $mapMatrix[0][1] = $rows;
+ $mapMatrix[0][2] = $checkpoints;
+ //set after;
+ $mapMatrix[0][3] = 0;
+ //walls
+ $mapMatrix[0][4] = $walls;
+ $mapMatrix[0][5] = $teleports;
+
+
+ do {
+ $rockcount = 0;
+ $i = 0;
+ if (! is_array($shape))
+ break;
+ foreach ($shape as $row) {
+ $i++;
+ for( $j = 0; $j < $cols; $j++) { //Number of Columns
+ $item = substr($row, $j, 1);
+ if ($item == '?') {
+ if (rand(1, $rockchance) == 1) {
+ $item = 'r';
+ $rockcount++;
+ } else {
+ $item = 'o';
+ }
+ }
+ $mapMatrix[$i][$j] = $item;
+ //echo $item;
+ }
+ //echo "\n";
+ }
+ $path = routePath($mapMatrix, '', true);
+ //echo $path['blocked']."\n";
+ //Only repeat if it's blocked.
+ } while ($path['blocked'] == true);
+
+ //Set rockcount.
+ $mapMatrix[0][3] = $rockcount;
+
+ //print_r ($mapMatrix);
+ return $mapMatrix;
+}
+
+//Inserts a point into a shape-array.
+function insertPoint($array, $new, $target = '?') {
+ //Replaces a random element in $array that matches $target with $new,
+ // if $new is a single character.
+ //If $new is a string, replace a random element for each of $new's characters.
+
+ /*
+ Snap - "the goal is to turn something like:
+ $mymap = Array("s??????f". "s???????f")
+ // into; Array("s???a??f", "s???b???f")
+ after a couple calls to the function."
+ */
+
+ //Getting $array parameters
+ $rows = count($array);
+ $length = strlen($array[0]);
+ $size = $rows * $length;
+
+ //Retrieving all cells == $target from $array
+ //This is more memory intensive, and probably more CPU-so, but far more safe.
+ $targetCells = array();
+
+ for( $i = 0; $i < $size; $i++)
+ {
+ $y = $i % $length; //Get coordinates based on $index
+ $x = (int)($i / $length);
+
+ //echo "<br />index: $i y: $y, x: $x <br />";
+ //echo $array[$x][$y];
+ if($array[$x][$y] == $target)
+ $targetCells[] = $i;
+ }
+
+ //Now that we have a definite selection of cells, we can pick easily with
+ // the only caveat being if there aren't enough $target cells.
+
+ if(strlen($new) > $length)
+ {
+ //There are too many replace requests and not enough free cells.
+ echo "Too many $new characters. No replacements made.<br/>";
+ return $array;
+ }
+
+ //Debug to see what indexes were selected as == $target
+ /*
+ echo "array = ";
+ print_r($array);
+ echo "<br/>";
+ echo "targetCells = ";
+ print_r($targetCells);
+ echo "<br/>";
+ echo "<br/>";
+ */
+
+ while(strlen($new) > 0)
+ {
+ $length = count($targetCells);
+ $indexReplace = rand(0, $length - 1); //Get a random position in $targetCells
+ $indexTarget = $targetCells[$indexReplace]; //Get the index stored in $targetCells
+
+ $y = $indexTarget % strlen($array[0]); //Get coordinates based on $index
+ $x = (int)($indexTarget / strlen($array[0]));
+
+ if($array[$x][$y] == $target) //Shouldn't be necessary, but just in case...
+ {
+ $array[$x][$y] = $new[0]; //We've found a valid target. Replace.
+ $new = substr($new, 1); //Go to the next $new character.
+
+ unset($targetCells[$indexReplace]); //Remove the selected $targetCell.
+ $targetCells = array_values($targetCells); //Reorder the array for no holes.
+
+ continue;
+ }
+ else
+ {
+ //Somehow an invalid target got into our list of valid targets...
+ echo "<br/>";
+ echo "Error in selecting $indexTarget ($x,$y). Replacement stopped on $new[0].<br/>";
+ echo "indexTarget = $indextarget<br/>";
+ echo "indexReplace = $indexReplace<br/>";
+
+ echo "array = ";
+ print_r($array);
+ echo "<br/>";
+ echo "targetCells = ";
+ print_r($targetCells);
+ echo "<br/>";
+ echo "<br/>";
+ return $array;
+ }
+ }
+
+ return $array;
+}
//Turns a mapMatrix into a code - see GenerateMapByCode
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 +505,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 +543,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 +551,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,52 +560,21 @@ 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.
@@ -489,8 +663,16 @@ function pastMap($maptype, $daysago) { return $r;
}
-
-
+function findTiles ($mapMatrix, $search) {
+ //print_r ($mapMatrix);
+ 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 )
@@ -512,45 +694,46 @@ function findPath($mapMatrix, $start = '0,1.', $target = 'f') { $y = $v[1];
//Create a handle on the squares around it.
switch($i){
- case 1: $y--; break; //up
- case 2: $x++; break; //right
- case 3: $y++; break; //down
- case 4: $x--; break; //left
+ case 1: $y--; break; //up
+ case 2: $x++; break; //right
+ case 3: $y++; break; //down
+ case 4: $x--; break; //left
}
if ($y < 1 OR $x < 0) continue 1;
//What's there?
switch($mapMatrix[$y][$x]) {
- case $target: //Finishline!
- //Our search is over.
- $r['blocked'] = false;
- $r['path'] = $seed[$key][3].$i;
- $r['start'] = $v[2];
- $r['end'] = "$x,$y";
- return $r;
+ case $target: //Finishline!
+ //Our search is over.
+ $r['blocked'] = false;
+ $r['path'] = $seed[$key][3].$i;
+ $r['start'] = $v[2];
+ $r['end'] = "$x,$y";
+ return $r;
break;
- // Teleports m t g i k
- case "m": case "t": case "g": case "i": case "k":
- $path = $mapMatrix[$y][$x];
-
- case "o": //Available squares
- //!!
- case "s": case "f": //Start and end tiles
+ // Teleports m t g i k
+ case "m": case "t": case "g": case "i": case "k":
+ $path = $mapMatrix[$y][$x];
- case "a": case "b": case "c": case "d": case "e": //Checkpoints too
- case "u": case "n": case "h": case "j": case "l": //Teleport-out towers included!
- //Plant Seed here
- $seed[$index][0] = $x;
- $seed[$index][1] = $y;
- //Save our starting position.
- $seed[$index][2] = $v[2];
- //Save 'PATH'
- $path = $i.$path;
- $seed[$index][3] = $v[3].$path;
- $path = '';
- //Been there, done that.
- $mapMatrix[$y][$x] = null;
- //Move index
- $index++;
+ case "o": //Available squares
+ //!!
+ case "p":
+ case "s": case "f": //Start and end tiles
+
+ case "a": case "b": case "c": case "d": case "e": //Checkpoints too
+ case "u": case "n": case "h": case "j": case "l": //Teleport-out towers included!
+ //Plant Seed here
+ $seed[$index][0] = $x;
+ $seed[$index][1] = $y;
+ //Save our starting position.
+ $seed[$index][2] = $v[2];
+ //Save 'PATH'
+ $path = $i.$path;
+ $seed[$index][3] = $v[3].$path;
+ $path = '';
+ //Been there, done that.
+ $mapMatrix[$y][$x] = null;
+ //Move index
+ $index++;
break;
}
@@ -585,61 +768,61 @@ function GetTile($mapMatrix, $id) //Routes a path through all checkpoints and teleports, returning an array.
// [path] path-string. [blocked] boolean, [moves] int.
-function routePath($mygrid, $start = '') {
+function routePath($mygrid, $start = '', $validate = false) {
- //== This should grab the start positions by scaning the map.
- if ($start == '') {
- for ($i = 1; $i <= $mygrid[0][1]; $i++) {
- $start .= "0,$i.";
- }
- }
+ //Locate the start locations.
+ $start = findTiles($mygrid, "s");
//Checkpoint names
- $cpnames = Array("a", "b", "c", "d", "e");
- //Get the amount of checkpoints on this map.
- $cpcount = $mygrid[0][2];
+ $cpnames = array("a", "b", "c", "d", "e");
- //Add the existing checkpoints to targets.
- for($p = 0; $p < $cpcount; $p++) {
- $target[] = $cpnames[$p];
- }
- //Always need the finish line.
+
+ //Add the existing checkpoints to target array.
+ foreach ($cpnames as $cpt)
+ if (findTiles($mygrid, $cpt))
+ $target[] = $cpt;
+
+ //Our last target is the finish line.
$target[] = 'f';
//Assume that we're not blocked, and raise a red flag later.
$blocked = false;
-
//All possible teleports in play.
- $tpnames = Array('t', 'm', 'g', 'i', 'k');
-
- $tpcount = intval($mygrid[0][5] * .5);
- //Add the existing checkpoints to targets.
- for($p = 0; $p < $tpcount; $p++) {
- $teleport[] = $tpnames[$p];
- }
+ $tpnames = array('t', 'm', 'g', 'i', 'k');
+ //Where there's an in, there's an out.
$teleout['t'] = 'u';
$teleout['m'] = 'n';
$teleout['g'] = 'h';
$teleout['i'] = 'j';
$teleout['k'] = 'l';
- //$r['tparray'] = $teleport;
- //$r['cparray'] = $target;
+ $teleport = array();
+
+ //Find all the existing teleports.
+ foreach ($tpnames as $tpt)
+ if (findTiles($mygrid, $tpt))
+ $teleport[] = $tpt;
+
+ //If validate, add teleport in/out's.
+ if ($validate) {
+ $target = array_merge($target, $teleport);
+ foreach ($teleport as $tin)
+ $target[] = $teleout[$tin];
+ }
//Loop through all the targets.
foreach($target as $t) {
//Path from where we are, to the target.
$p = Findpath ($mygrid, $start, $t);
+ //echo "FP: ".$p['path'];
+ //echo "T: $t";
//It's possible to start from multiple places;
//so mark where we ended up starting from.
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 +855,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.
@@ -696,6 +880,8 @@ function routePath($mygrid, $start = '') { //123 U CORDS U
//123 U CORDS U 2322
$p['path'] .= $z['path'];
+ //Our end point may have been modified.
+ $p['end'] = $z['end'];
$movesoffset -= countmoves($out);
//}
|