summaryrefslogtreecommitdiffstats
path: root/includes
diff options
context:
space:
mode:
Diffstat (limited to 'includes')
-rw-r--r--includes/header.php2
-rw-r--r--includes/mapoftheday.php2
-rw-r--r--includes/maps.php434
3 files changed, 312 insertions, 126 deletions
diff --git a/includes/header.php b/includes/header.php
index 53c1a82..b386cfa 100644
--- a/includes/header.php
+++ b/includes/header.php
@@ -9,7 +9,7 @@ function htmlHeader() {
<link href="css/mapstyle.css" rel="stylesheet" type="text/css" />
<link href="css/pagestyle.css" rel="stylesheet" type="text/css" />
<link href="css/statsstyle.css" rel="stylesheet" type="text/css" />
- <title>Snapems.com Mazegame</title>
+ <title>pathery.com</title>
<script src="js/ajax.js" type="text/javascript"></script>
<script src="js/mapspecs.js" type="text/javascript"></script>
diff --git a/includes/mapoftheday.php b/includes/mapoftheday.php
index cd646cb..62b44a4 100644
--- a/includes/mapoftheday.php
+++ b/includes/mapoftheday.php
@@ -1 +1 @@
-<?PHP include_once('maps.php'); include_once('db.inc.php'); //Select from current day. function getMapOfTheDay() { $sql = " select `ID`, `code` from `maps` WHERE ( DAY(dateCreated) = DAY(NOW()) AND MONTH(dateCreated) = MONTH(NOW()) AND YEAR(dateCreated) = YEAR(NOW()) ) "; $result = mysql_query($sql); //No map for today? if (mysql_num_rows($result) == 0) { // If you want to modify the maps created! This is the line $map = GenerateMap(rand(13, 18), rand(10, 14), rand(6, 9)); $code = GenerateMapCode($map); $sql = "INSERT INTO `maps` (`code`) VALUES ('$code')"; mysql_query($sql); $r['code'] = $code; $r['map'] = $map; $r['id'] = mysql_insert_id(); return $r; } $r['code'] = mysql_result($result, 0, 'code'); $r['map'] = GenerateMapByCode($r['code']); $r['id'] = mysql_result($result, 0, 'ID'); return $r; } function mapOfTheDay($type = 1) { $sql = " select maps.ID, maps.code FROM `mapOfTheDay`, `maps` WHERE `mapDate` = CURDATE() AND `mapType` = $type AND mapID = maps.ID "; $result = mysql_query($sql); //No map for today? if (mysql_num_rows($result) == 0) { // If you want to modify the maps created! This is the line //GenerateMap($rows, $cols, $rockchance, $numBlocks = -1, $cp = -1, $tp = -1) { switch ($type) { case 1: //Easy $map = GenerateMap(13, 7, 12, rand(7,10), rand(0, 2), 0); break; case 2: //Normal $map = GenerateMap(15, 9, 7, rand(11,13), rand(1,2) + rand(0,1), rand(0,1)); break; case 3: //Hard $map = GenerateMap(19, 9, rand(7, 9), rand(13,16), rand(2,5), rand(1,2)); break; case 4: //Full random map //Get the current day as int. $numday = date('w'); $numday = intval($numday); switch ($numday) { case 0: //Sunday //Thirty $map = GenerateMap( 18, 14, 20 , //width, height, rocks weight(30) , //Walls weight(1) , //Checkpoints weight(1) //Teleports ); break; case 1: //Monday //Simple $map = GenerateMap( 18, 9, 7 , //width, height, rocks weight(15,16,17) , //Walls weight(0) , //Checkpoints weight(0) //Teleports ); break; case 2: //Tuesday //ABC's $map = GenerateMap( 19, 11, 12 , //width, height, rocks weight(20,21,22,22,23) , //Walls weight(3) , //Checkpoints weight(0) //Teleports ); break; case 3: //Wednesday //Tele Madness $map = GenerateMap( 17, 12, 10 , //width, height, rocks weight(17,18) , //Walls weight(1) , //Checkpoints weight(5) //Teleports ); break; case 4: //Thursday //Rocky Maze $map = GenerateMap( 19, 15, 5 , //width, height, rocks weight(16,17,18) , //Walls weight(1,2,2,2,3,3) , //Checkpoints weight(0) //Teleports ); break; case 5: //Friday //Side to Side $map = GenerateMap( 26, 6, 12 , //width, height, rocks weight(17,18,19) , //Walls weight(2,2,2,3,3) , //Checkpoints weight(3,3,3,4) //Teleports ); break; case 6: //Saturday //ABC's $map = GenerateMap( 19, 11, 12 , //width, height, rocks weight(20,21,22,22,23) , //Walls weight(3) , //Checkpoints weight(0) //Teleports ); break; } break; default: $map = GenerateMap(rand(13, 18), rand(10, 14), rand(6, 9)); break; } $code = GenerateMapCode($map); $sql = "INSERT INTO `maps` (`code`) VALUES ('$code')"; mysql_query($sql); $mapID = mysql_insert_id(); $r['code'] = $code; $r['map'] = $map; $r['id'] = $mapID; $sql = "INSERT INTO `mapOfTheDay` (`mapID`, `mapType`, `mapDate`) VALUES ('$mapID', '$type', CURDATE()) "; mysql_query($sql); return $r; } $r['code'] = mysql_result($result, 0, 'code'); $r['map'] = GenerateMapByCode($r['code']); $r['id'] = mysql_result($result, 0, 'ID'); return $r; } // RMA # CT1160811 // 97686 //Select from yesterday function getYesterdaysMap() { $sql = " select `ID`, `code` from `maps` WHERE ( DAY(dateCreated) = DAY(NOW()) - 1 AND MONTH(dateCreated) = MONTH(NOW()) AND YEAR(dateCreated) = YEAR(NOW()) ) "; $result = mysql_query($sql); //No map for today? if (mysql_num_rows($result) == 0) { return -1; } $r['code'] = mysql_result($result, 0, 'code'); $r['id'] = mysql_result($result, 0, 'ID'); return $r; } ?> \ No newline at end of file
+<?PHP include_once('maps.php'); include_once('db.inc.php'); //Select from current day. function getMapOfTheDay() { $sql = " select `ID`, `code` from `maps` WHERE ( DAY(dateCreated) = DAY(NOW()) AND MONTH(dateCreated) = MONTH(NOW()) AND YEAR(dateCreated) = YEAR(NOW()) ) "; $result = mysql_query($sql); //No map for today? if (mysql_num_rows($result) == 0) { // If you want to modify the maps created! This is the line $map = GenerateMap(rand(13, 18), rand(10, 14), rand(6, 9)); $code = GenerateMapCode($map); $sql = "INSERT INTO `maps` (`code`) VALUES ('$code')"; mysql_query($sql); $r['code'] = $code; $r['map'] = $map; $r['id'] = mysql_insert_id(); return $r; } $r['code'] = mysql_result($result, 0, 'code'); $r['map'] = GenerateMapByCode($r['code']); $r['id'] = mysql_result($result, 0, 'ID'); return $r; } function mapOfTheDay($type = 1) { $sql = " select maps.ID, maps.code FROM `mapOfTheDay`, `maps` WHERE `mapDate` = CURDATE() AND `mapType` = $type AND mapID = maps.ID "; $result = mysql_query($sql); //No map for today? if (mysql_num_rows($result) == 0) { // If you want to modify the maps created! This is the line //GenerateMap($rows, $cols, $rockchance, $numBlocks = -1, $cp = -1, $tp = -1) { switch ($type) { case 1: //Easy $map = GenerateMap(13, 7, 12, rand(7,10), rand(0, 2), 0); break; case 2: //Normal $map = GenerateMap(15, 9, 7, rand(11,13), rand(1,2) + rand(0,1), rand(0,1)); break; case 3: //Hard $map = GenerateMap(19, 9, rand(7, 9), rand(13,16), rand(2,5), rand(1,2)); break; case 4: //Full random map //Get the current day as int. $numday = date('w'); $numday = intval($numday); switch ($numday) { case 0: //Sunday //Thirty $map = GenerateMap( 18, 14, 20 , //width, height, rocks weight(30) , //Walls weight(1) , //Checkpoints weight(1) //Teleports ); break; case 1: //Monday //Simple $map = GenerateMap( 18, 9, 7 , //width, height, rocks weight(15,16,17) , //Walls weight(0) , //Checkpoints weight(0) //Teleports ); break; case 2: //Tuesday //ABC's $map = GenerateMap( 19, 11, 12 , //width, height, rocks weight(20,21,22,22,23) , //Walls weight(3) , //Checkpoints weight(0) //Teleports ); break; case 3: //Wednesday //Tele Madness $map = GenerateMap( 17, 12, 10 , //width, height, rocks weight(17,18) , //Walls weight(1) , //Checkpoints weight(5) //Teleports ); break; case 4: //Thursday //Rocky Maze $map = GenerateMap( 19, 15, 5 , //width, height, rocks weight(16,17,18) , //Walls weight(1,2,2,2,3,3) , //Checkpoints weight(0) //Teleports ); break; case 5: //Friday //Side to Side $map = GenerateMap( 26, 6, 12 , //width, height, rocks weight(17,18,19) , //Walls weight(2,2,2,3,3) , //Checkpoints weight(3,3,3,4) //Teleports ); break; case 6: //Saturday //Dual map $dualmapa[] = "s???????????????????f"; $dualmapa[] = "s???????????????????f"; $dualmapa[] = "s???????????????????f"; $dualmapa[] = "s???????????????????f"; $dualmapa[] = "s???????????????????f"; $dualmapa[] = "s???????????????????f"; $dualmapa[] = "oqqqqqqqqqqqqqqqqqqqq"; $dualmapa[] = "oqqqqqqqqqqqqqqqqqqqq"; $dualmapb[] = "s???????????????????f"; $dualmapb[] = "s???????????????????f"; $dualmapb[] = "s???????????????????f"; $dualmapb[] = "s???????????????????f"; $dualmapb[] = "s???????????????????f"; $dualmapb[] = "s???????????????????f"; $dualmapa = insertPoint($dualmapa, 'abc'); $dualmapb = insertPoint($dualmapb, 'abc'); $dualmap = array_merge($dualmapa, $dualmapb); $dualmap = insertPoint($dualmap, 'tu'); $myparams['checkpoints'] = 3; $myparams['teleports'] = 1; $myparams['rockchance'] = 9; $myparams['walls'] = weight(20, 20, 21, 21, 22, 23); $map = GenerateShapedMap($dualmap, $myparams); break; } break; default: $map = GenerateMap(rand(13, 18), rand(10, 14), rand(6, 9)); break; } $code = GenerateMapCode($map); $sql = "INSERT INTO `maps` (`code`) VALUES ('$code')"; mysql_query($sql); $mapID = mysql_insert_id(); $r['code'] = $code; $r['map'] = $map; $r['id'] = $mapID; $sql = "INSERT INTO `mapOfTheDay` (`mapID`, `mapType`, `mapDate`) VALUES ('$mapID', '$type', CURDATE()) "; mysql_query($sql); return $r; } $r['code'] = mysql_result($result, 0, 'code'); $r['map'] = GenerateMapByCode($r['code']); $r['id'] = mysql_result($result, 0, 'ID'); return $r; } // RMA # CT1160811 // 97686 //Select from yesterday function getYesterdaysMap() { $sql = " select `ID`, `code` from `maps` WHERE ( DAY(dateCreated) = DAY(NOW()) - 1 AND MONTH(dateCreated) = MONTH(NOW()) AND YEAR(dateCreated) = YEAR(NOW()) ) "; $result = mysql_query($sql); //No map for today? if (mysql_num_rows($result) == 0) { return -1; } $r['code'] = mysql_result($result, 0, 'code'); $r['id'] = mysql_result($result, 0, 'ID'); return $r; } ?> \ No newline at end of file
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);
//}