summaryrefslogtreecommitdiffstats
path: root/includes/maps.php
diff options
context:
space:
mode:
authorPatrick Davison <snapwilliam@gmail.com>2011-11-20 14:11:50 -0800
committerPatrick Davison <snapwilliam@gmail.com>2011-11-20 14:11:50 -0800
commit8c29ba84cc98b60a20e8e79db37eeb607dc7a6db (patch)
tree53f5ee7771025c1dc7645237ff6e718d911379f2 /includes/maps.php
parent4dfa3bb62317d1268eaf84b94fd19c97e36e1796 (diff)
downloadpathery-8c29ba84cc98b60a20e8e79db37eeb607dc7a6db.tar.xz
Efficiency improvements to findPath
Diffstat (limited to 'includes/maps.php')
-rw-r--r--includes/maps.php26
1 files 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++;