diff options
author | raylu <raylu@cmu.edu> | 2011-04-07 00:27:27 -0400 |
---|---|---|
committer | raylu <raylu@cmu.edu> | 2011-04-07 00:27:27 -0400 |
commit | f8c9eb5220afaf2f9a62f9a176a45913240f4081 (patch) | |
tree | 99775178acfcef6e86e39184be0ac96083042f1b /includes/maps.php | |
download | pathery-f8c9eb5220afaf2f9a62f9a176a45913240f4081.tar.xz |
Initial import from Dropbox
Diffstat (limited to 'includes/maps.php')
-rw-r--r-- | includes/maps.php | 743 |
1 files changed, 743 insertions, 0 deletions
diff --git a/includes/maps.php b/includes/maps.php new file mode 100644 index 0000000..acb4c6c --- /dev/null +++ b/includes/maps.php @@ -0,0 +1,743 @@ +<?PHP
+function DisplayMap($mapMatrix, $idprefix = 1, $example = false, $speed = NULL) {
+ //Iterate through $mapMatrix and generate the html
+ $maptable = ""; //The string to return to the database.
+ $index = 0; //The current number of tiles from the last tile saved.
+
+
+ if ($speed == NULL) {
+ if ($example) {
+ $speed = 1;
+ } else {
+ $speed = 2;
+ }
+ }
+
+ for($i = 1; $i < count($mapMatrix); $i++)
+ {
+ $maptable .= "<tr>";
+ for($j = 0; $j < count($mapMatrix[$i]); $j++)
+ {
+ //==
+ $index++;
+
+ $handle = "$idprefix,$i,$j";
+ switch($mapMatrix[$i][$j])
+ {
+ case 's': $maptable .= "<td class='grid_td_start' id='$handle' ></td>"; break;
+ case 'f': $maptable .= "<td class='grid_td_finish' id='$handle' ></td>"; break;
+
+ //TP1
+ case 't': $maptable .= "<td title='Teleport 1 in' class='grid_td_tp1_in' id='$handle' ></td>"; break;
+ case 'u': $maptable .= "<td title='Teleport 1 out' class='grid_td_tp1_out' id='$handle' ></td>"; break;
+ //TP2
+ case 'm': $maptable .= "<td title='Teleport 2 in' class='grid_td_tp2_in' id='$handle' ></td>"; break;
+ case 'n': $maptable .= "<td title='Teleport 2 out'class='grid_td_tp2_out' id='$handle' ></td>"; break;
+ //TP3
+ case 'g': $maptable .= "<td title='Teleport 3 in' class='grid_td_tp3_in' id='$handle' ></td>"; break;
+ case 'h': $maptable .= "<td title='Teleport 3 out'class='grid_td_tp3_out' id='$handle' ></td>"; break;
+ //TP4
+ case 'i': $maptable .= "<td title='Teleport 4 in' class='grid_td_tp4_in' id='$handle' ></td>"; break;
+ case 'j': $maptable .= "<td title='Teleport 4 out'class='grid_td_tp4_out' id='$handle' ></td>"; break;
+ //TP5
+ case 'k': $maptable .= "<td title='Teleport 5 in' class='grid_td_tp5_in' id='$handle' ></td>"; break;
+ case 'l': $maptable .= "<td title='Teleport 5 out'class='grid_td_tp5_out' id='$handle' ></td>"; break;
+
+ case 'a': $maptable .= "<td class='grid_td_cpa' id='$handle' ></td>"; break;
+ case 'b': $maptable .= "<td class='grid_td_cpb' id='$handle' ></td>"; break;
+ case 'c': $maptable .= "<td class='grid_td_cpc' id='$handle' ></td>"; break;
+ case 'd': $maptable .= "<td class='grid_td_cpd' id='$handle' ></td>"; break;
+ 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 '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>";
+ //default: $maptable .= "<td class='grid_td' id='$handle' onClick='grid_click(this)' >".$mapMatrix[$i][$j]."</td>";
+ }
+ }
+ $maptable .= "</tr>";
+ }
+ //Prepare mapdata.
+ $mapdata['height'] = $mapMatrix[0][0];
+ $mapdata['width'] = $mapMatrix[0][1];
+ $mapdata['points'] = $mapMatrix[0][2];
+ $mapdata['rocks'] = $mapMatrix[0][3];
+ $mapdata['walls'] = $mapMatrix[0][4];
+ $mapdata['teleports'] = $mapMatrix[0][5];
+ $mapdata['example'] = $example;
+ $mapdata['mapid'] = $idprefix;
+
+ $path = routePath($mapMatrix, '');
+ $mapdata['code'] = GenerateMapCode($mapMatrix);
+
+ $width = (($j * 35) + 2).'px';
+ //$width = (($j * 23) + 2).'px';
+ $i -= 1;
+ $height = (($i * 35)).'px';
+ //$height = (($i * 22) + 2).'px';
+
+ $jsonmap = json_encode($mapdata);
+ $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
+ </table>";
+
+
+ $prefSpeed = $_COOKIE['pref_speed'];
+ $speedOption['Slow'] = 1;
+ $speedOption['Med'] = 2;
+ $speedOption['Fast'] = 3;
+ $speedOption['Ultra'] = 4;
+ if (!in_array($prefSpeed, $speedOption))
+ $prefSpeed = '2';
+
+ foreach ($speedOption as $key => $value) {
+ $rOption .= "<option value='$value'";
+ if ($prefSpeed == $value)
+ $rOption .= " selected='selected'";
+ $rOption .= ">$key</option>\n";
+ }
+
+ if ($_COOKIE['pref_mute'] == "true") {
+ $mutebutton = "<label><input onclick='savePref(\"mute\", this.checked)' type='checkbox' id='$idprefix,mute' checked='checked' />Mute</label>";
+ } else {
+ $mutebutton = "<label><input onclick='savePref(\"mute\", this.checked)' type='checkbox' id='$idprefix,mute' />Mute</label>";
+ }
+
+
+ if ($example) {
+ $output = $maptable;
+ $output .= "<input id='$idprefix,btn' type='button' onclick='doSend($idprefix)' value='Test' />";
+ $output .= "
+ <div style='display:none;'>
+ <input type='checkbox' id='$idprefix,mute' checked=true />
+ <select id='$idprefix,speed'>
+ $rOption
+ </select>
+ </div>
+ ";
+ $output .= $mapdatadiv;
+ $output = "<div style='width:$width;height:$height;'>
+ $output
+ </div>";
+ return $output;
+ }
+ //$date = date("m-d-y");
+
+ $output = "
+ <div id='$idprefix,outer' class='grid_outer' style='width:".($width+2)."px;height:".($height+50)."px;'>
+
+ <div class='grid_dsp_left dsp_49'>
+ <div id='$idprefix,dspID' title='MapID: $idprefix'>
+ MapID: $idprefix
+ </div>
+ </div>
+
+
+ <div id='$idprefix,dsptr' class='grid_dsp_right dsp_33'>
+ <span id='$idprefix,dspWalls' class='grid_dsp_data'>
+ ".$mapdata['walls']." walls
+ </span>
+ <span>
+ ( <a href='javascript:resetwalls($idprefix)'>Reset</a> )
+ </span>
+ </div>
+
+
+ $maptable
+
+ <div id='$idprefix,dspbl' class='grid_dsp_left dsp_49'>
+ <input id='$idprefix,btn' type='button' onclick='doSend($idprefix)' value='Go!' />
+ Speed:
+ <select onChange='savePref(\"speed\", this.value)' id='$idprefix,speed'>
+ $rOption
+ </select>
+ </div>
+
+ <div class='grid_dsp_mid dsp_16'>
+ $mutebutton
+ </div>
+
+ <div id='$idprefix,dspbr' class='grid_dsp_right dsp_33'>
+ <div id='$idprefix,dspCount' class='grid_dsp_data'>
+ ".$path['moves']." moves
+ </div>
+ </div>
+
+ $mapdatadiv
+ </div>
+ ";
+
+ return $output;
+}
+//Generates map
+function GenerateMap($rows, $cols, $rockchance, $numBlocks = -1, $cp = -1, $tp = -1) {
+
+ //!! Possibility of inf loop here.
+ do {
+ $randvalue = rand(1, ($rows * $cols));
+ //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) % ($rows)) > 3) {
+ $unique[] = $randvalue;
+ $unique = array_unique($unique);
+ $unique = array_values($unique);
+ }
+ } while (count($unique) < 15);
+
+ 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.
+
+ $cpnames = Array("a", "b", "c", "d", "e");
+ $tpnames = Array("t", "u", "m", "n", 'g', 'h', 'i', 'j', 'k', 'l');
+
+ $teleport = Array();
+ $checkpoint = Array();
+
+ $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.
+
+ // We need to make sure the map we construct is valid.
+ // so we throw this in a do-while.
+ do {
+ $p = -1;
+ //Begin loop to populate grid.
+ for( $y = 1; $y <= $cols; $y++) { //Number of Columns
+ for( $x = 0; $x < $rows; $x++) { //Number of Rows
+ $p++;
+ //Start and Finish squares.
+ if ($x == 0) {
+ $grid[$y][$x] = "s";
+ } elseif ($x == $rows - 1) {
+ $grid[$y][$x] = "f";
+ //Randomly Placed Rocks
+ } elseif (rand(1, $rockchance) == 2) {
+ $grid[$y][$x] = "r";
+ $rocks++;
+ //!! 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;
+ }
+ } //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] = $rows;
+ $grid[0][1] = $cols;
+ $grid[0][2] = count($checkpoint);
+ $grid[0][3] = $rocks;
+ $grid[0][4] = $numBlocks;
+ $grid[0][5] = count($teleport);
+
+ //Confirm the map isn't broken to start-out.
+ $path = routePath($grid);
+ //Only repeat if it's blocked.
+ } while ($path['blocked'] == true);
+
+ return $grid;
+}
+
+
+//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.
+
+ // $mapMatrix[0] stores header data--dimensions, #checkpoints, #rocks, #walls
+ // $mapMatrix[1] count will always be the width.
+ $mapsize = $mapMatrix[0][0].'x'.$mapMatrix[0][1]; //Width x Height
+
+ $code = $mapsize.
+ '.c'.$mapMatrix[0][2].
+ '.r'.$mapMatrix[0][3].
+ '.w'.$mapMatrix[0][4].
+ '.t'.$mapMatrix[0][5];
+ // dimensions + # checkpoints + # rocks + # placeable walls
+
+ //echo $code."<br />";
+
+ $code .= ".:";
+
+ for( $i = 1; $i < count($mapMatrix); $i++)
+ {
+ for( $j = 0; $j < count($mapMatrix[$i]); $j++)
+ {
+ if($mapMatrix[$i][$j] != 'o')
+ {
+ //As long as the tile is NOT open, embed it in the code.
+ $code .= $index.$mapMatrix[$i][$j].'.';
+ $index = -1;
+ //==echo "<br /><b>".$index.$mapMatrix[$i][$j]."</b><br />";
+ }
+ $index += 1;
+ }
+ }
+
+ return $code;
+}
+
+//Turns a mapcode into a mapMatrix, - see GenerateMapCode
+function GenerateMapByCode($code) {
+ //Create $mapMatrix by iterating through $code (a string value).
+ //==$mapMatrix = array();
+
+
+ $tmp = explode( ":", $code);
+
+ $headers = explode( '.', $tmp[0]);
+ $splitCode = explode( '.', $tmp[1]);
+
+ //Extract header information.
+ //==$mapMatrix[0] = array();
+ $dimensions = explode( 'x', $headers[0]);
+ $mapMatrix[0][0] = $dimensions[0]; //Width
+ $mapMatrix[0][1] = $dimensions[1]; //Height
+
+ //Select the next parameters by ignoring the character label.
+ $mapMatrix[0][2] = (int)substr($headers[1], 1); //Number of Checkpoints
+ $mapMatrix[0][3] = (int)substr($headers[2], 1); //Number of Rocks
+ $mapMatrix[0][4] = (int)substr($headers[3], 1); //Number of Wall Blocks
+ $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 />";
+ echo "mapMatrix[0][2]: ".$mapMatrix[0][2]."<br />";
+ 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;
+ for( $i = 1; $i <= $mapMatrix[0][1]; $i++) { //Number of Rows
+ for( $j = 0; $j < $mapMatrix[0][0]; $j++) { //Number of Columns
+ $t++;
+ $next = substr($splitCode[$index], 0, strlen($splitCode[$index]) - 1);
+
+ if ($next == $t) {
+ $type = substr($splitCode[$index], -1, 1);
+ $mapMatrix[$i][$j] = $type;
+ $index++;
+ $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;
+ $sa = explode( '.', $solution);
+ foreach($sa as $v) {
+ if ($v == '') continue;
+ $v= explode(",", $v);
+ $i = $v[0];
+ $j = $v[1];
+
+ //Trying to place a wall - where?
+ if ($mapMatrix[$i][$j] <> 'o') return -1;
+ $mapMatrix[$i][$j] = 'w';
+
+ //Are we out of blocks?
+ //if ($mapMatrix[0][2] < 1) return -2;
+ $mapMatrix[0][4]--;
+ }
+ return $mapMatrix;
+}
+
+function seperateMapSolution($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] == 'w')
+ $solution .= "$i,$j.";
+ $solution = ".".$solution;
+ return $solution;
+}
+
+//This is required to identify identical solutions, or even 'close' ones.
+function formSolution($solution) {
+ $tmp = explode(".", $solution);
+ $tmp = array_filter($tmp);
+ sort($tmp);
+ $tmp = '.'.implode(".", $tmp).'.';
+ return $tmp;
+}
+
+//Returns the best solution.
+function getSolution($userID, $mapID) {
+ include_once('db.inc.php');
+ $sql = "SELECT `solution`
+ FROM `solutions`
+ WHERE `userID` = '$userID' AND
+ `mapID` = '$mapID'
+ ";
+ $result = mysql_query($sql);
+ if (mysql_num_rows($result) > 0) {
+ list($solution) = mysql_fetch_row($result);
+ return $solution;
+ }
+}
+
+function getMapCode($mapID) {
+ include_once('db.inc.php');
+ $sql = "SELECT `code`
+ FROM `maps`
+ WHERE `ID` = '$mapID'
+ ";
+ $result = mysql_query($sql);
+ if (mysql_num_rows($result) > 0) {
+ list($map) = mysql_fetch_row($result);
+ return $map;
+ }
+}
+
+function pastMap($maptype, $daysago) {
+ $sql = "
+ SELECT `mapID`
+ FROM `mapOfTheDay`
+ WHERE DATE_ADD(CURDATE(), INTERVAL -$daysago DAY) =
+ DATE_FORMAT(mapDate,'%Y-%m-%d') AND
+ `mapType` = '$maptype'
+ ";
+ //echo "<br />$sql<br />";
+ $result = mysql_query($sql) or die(mysql_error());
+ //No map for today?
+ if (mysql_num_rows($result) == 0)
+ return -1;
+
+ $r = mysql_result($result, 0, 'mapID');
+ //echo "result: $r";
+ return $r;
+}
+
+
+
+
+
+// Returns: ARRAY( blocked, path, start, end )
+function findPath($mapMatrix, $start = '0,1.', $target = 'f') {
+ $seed = explode(".", $start);
+ foreach ($seed as &$v) {
+ $v = explode(",", $v);
+ $v[2] = $v[0].','.$v[1];
+ }
+ //print_r ($seed);
+
+ $index = count($seed);
+
+ do {
+ foreach ($seed as $key => $v) {
+ //Search the squares around, to spread the seeds
+ for($i = 1; $i <= 4; $i++) {
+ $x = $v[0];
+ $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
+ }
+ 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;
+ 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
+
+ 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;
+ }
+ }
+ //Running out of seeds?
+ if (count($seed) < 2) {
+ $r['blocked'] = true;
+ $r['path'] = $seed[$key][3].$i;
+ $r['end'] = "$x,$y";
+ return $r;
+ }
+ //Lets not try this again.
+ unset($seed[$key]);
+ }
+ } while ( 1);
+ echo "Ran outa seeds.<br />";
+ print_r($seed);
+ return $mapMatrix;
+}
+
+
+/* UNUSED FUNCTION
+function GetTile($mapMatrix, $id)
+{
+ //Returns the location in $mapMatrix indicated by $id
+ $toReturn = &$mapMatrix [ (int)($id / $mapMatrix[0][0]) ]
+ [ $id % $mapMatrix[0][0] ];
+
+ return $toReturn;
+}
+ */
+
+//Routes a path through all checkpoints and teleports, returning an array.
+// [path] path-string. [blocked] boolean, [moves] int.
+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.";
+ }
+ }
+
+ //Checkpoint names
+ $cpnames = Array("a", "b", "c", "d", "e");
+ //Get the amount of checkpoints on this map.
+ $cpcount = $mygrid[0][2];
+
+ //Add the existing checkpoints to targets.
+ for($p = 0; $p < $cpcount; $p++) {
+ $target[] = $cpnames[$p];
+ }
+ //Always need 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];
+ }
+
+ $teleout['t'] = 'u';
+ $teleout['m'] = 'n';
+ $teleout['g'] = 'h';
+ $teleout['i'] = 'j';
+ $teleout['k'] = 'l';
+
+ //$r['tparray'] = $teleport;
+ //$r['cparray'] = $target;
+
+ //Loop through all the targets.
+ foreach($target as $t) {
+ //Path from where we are, to the target.
+ $p = Findpath ($mygrid, $start, $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))
+ break 1;
+ //Search through the path to find first teleport hit, if any.
+
+ $pathary = str_split($p['path']);
+
+ //As per usual, it's best to assume that you've failed.
+ $foundtele = false;
+ //Search through pathary and compare all existing teleports.
+ foreach ($pathary as $position => $char) {
+ foreach ($teleport as $ktel => $port) {
+ //$r['z'] .= ';'.$f++;
+ if ($port == $char) {
+ //Disable teleport
+ //$teleport[$ktel] = '';
+ unset($teleport[$ktel]);
+ //$teleport = array_values($teleport);
+ //We found it
+ $foundtele = true;
+ break 2;
+ }
+ }
+ }
+ if ($foundtele == false) {
+ break 1;
+ }
+
+
+ $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'];
+ if ($x['blocked']) $blocked = true; //Optional?
+
+ //New path starting from our out-location.
+ $z = Findpath($mygrid, $out, $t);
+ if ($z['blocked']) $blocked = true; //Optional?
+
+ //Deactivate teleport
+ $teleactive[$port] = false;
+ //$r['debug'] = "TPD: $port";
+
+ //Apply modified path, and warp-cordinates.
+ //123 _Tele_ 222
+ //123
+ $p['path'] = substr($p['path'], 0, $position + 1);
+ //123
+ //123 U CORDS U
+ $p['path'] .= $outchar.$out.$outchar;
+ //123 U CORDS U
+ //123 U CORDS U 2322
+ $p['path'] .= $z['path'];
+
+ $movesoffset -= countmoves($out);
+ //}
+ } while ($foundtele);
+
+ //$start = explode(",", $p['end']);
+ $start = $p['end'];
+ if ($p['blocked']) $blocked = true;
+ $tpath .= $t.$p['path'].'r';
+ }
+ $r['blocked'] = $blocked;
+ $r['path'] = $tpath;
+ //$moves = count_chars($tpath, 0);
+ $moves = countmoves($tpath);
+ $moves += $movesoffset;
+ $r['moves'] = $moves;
+ //$r['moves'] = print_r($moves);
+ return $r;
+}
+//For use with routepath;
+function cmp($a, $b) {
+ if ($a === false) $a = 10000;
+ if ($b === false) $b = 10000;
+ if ($a == $b) {
+ return 0;
+ }
+ return ($a < $b) ? -1 : 1;
+}
+//For use with routepath;
+function countmoves($path) {
+ $moves = substr_count($path, '1');
+ $moves += substr_count($path, '2');
+ $moves += substr_count($path, '3');
+ $moves += substr_count($path, '4');
+ return $moves;
+}
+
+//Returns a random selection of one of the arguements.
+function weight() {
+ $weights = func_get_args();
+ return $weights[rand(0, (count($weights) -1))];
+}
+
+
+?>
|