From 8c29ba84cc98b60a20e8e79db37eeb607dc7a6db Mon Sep 17 00:00:00 2001 From: Patrick Davison Date: Sun, 20 Nov 2011 14:11:50 -0800 Subject: Efficiency improvements to findPath --- includes/maps.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/includes/maps.php b/includes/maps.php index f3ac29c..d50175a 100644 --- a/includes/maps.php +++ b/includes/maps.php @@ -720,8 +720,9 @@ function pastMap($maptype, $daysago) { return $r; } +//Returns tiles as a string seperated like: x,y.x,y. function findTiles ($mapMatrix, $search) { - //print_r ($mapMatrix); + $r = false; 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) @@ -734,13 +735,19 @@ function findTiles ($mapMatrix, $search) { // Returns: ARRAY( blocked, path, start, end ) //!! X and Y are reversed here... -function findPath($mapMatrix, $start = '0,1.', $target = 'f') { +function findPath($mapMatrix, $start = '0,1', $target = 'f') { + + //Remove last period if existing. + if (substr($start, -1) == '.') + $start = substr ($start, 0, -1); + + //Create our starting locations $seed = explode(".", $start); foreach ($seed as &$v) { $v = explode(",", $v); $v[2] = $v[0].','.$v[1]; + $v[3] = ''; } - //print_r ($seed); $index = count($seed); @@ -757,7 +764,15 @@ function findPath($mapMatrix, $start = '0,1.', $target = 'f') { case 3: $y++; break; //down case 4: $x--; break; //left } + //Ensure we don't enter into our mapdata area if ($y < 1 OR $x < 0) continue 1; + + //Is this tile even on the map? + if (!isset($mapMatrix[$y][$x])) { + continue; + } + $path = ''; + //What's there? switch($mapMatrix[$y][$x]) { case $target: //Finishline! @@ -787,9 +802,8 @@ function findPath($mapMatrix, $start = '0,1.', $target = 'f') { //Save 'PATH' $path = $i.$path; $seed[$index][3] = $v[3].$path; - $path = ''; - //Been there, done that. - $mapMatrix[$y][$x] = null; + //Don't plant a seed here again. + unset($mapMatrix[$y][$x]); //Move index $index++; -- cgit v1.2.3