ClarkeRubber

Seems fine to me.

Feb 15th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2. include("systems/systems.php");
  3. $PossiblePaths = array();
  4. $PathCount = 0;
  5. function PlanRoute($nextID, $targetID, $history = null, $depth = 0){
  6.     global $Systems, $PossiblePaths, $PathCount;
  7.     $potentials = $Systems[$nextID]["JumpsTo"];
  8.     $history[] = $nextID;
  9.     if(!empty($potentials)){
  10.         if(!in_array($targetID, $potentials)){ //Not at end location, therefore try next location
  11.             $depth++;
  12.             if($depth < 20){
  13.                 foreach($potentials as $value){
  14.                     if(!in_array($value, $history)){
  15.                         $path = PlanRoute($value, $targetID, $history, $depth);
  16.                         if(isset($path)){
  17.                             $PathCount++;
  18.                             $PossiblePaths[$PathCount] = $path;
  19.                         }
  20.                     }
  21.                 }
  22.             }else{
  23.                 return;
  24.             }
  25.         }else{
  26.             foreach($history as $SystemID){
  27.                 $Trip[] = $SystemID;
  28.             }
  29.             $Trip[] = $targetID;
  30.             return $Trip;
  31.         }
  32.     }
  33. }
  34. function SortByElementCount($a, $b){
  35.     if(count($a) == count($b)){
  36.         return 0;
  37.     }
  38.     return (count($a) < count($b)) ? -1 : 1;
  39. }
  40.  
  41. function SortBySecurity($a, $b){
  42.     $countA = count($a);
  43.     $countB = count($b);
  44.     foreach($a as $ae){
  45.         $averageA += $Systems[$ae]['Sec']/$countA;
  46.     }
  47.     foreach($b as $be){
  48.         $averageB += $Systems[$be]['Sec']/$countB;
  49.     }
  50.     if($averageB == $averageA){
  51.         return 0;
  52.     }
  53.     return ($averageA < $averageB) ? -1 : 1;
  54. }
  55. $origin = 30000001; //Get starting point
  56. $target = 30000142; //Get end point
  57. echo "Planning jump route from ".$Systems[$origin]['Name']." to ".$Systems[$target]['Name']."<br><br>\n";
  58. $path = PlanRoute($origin, $target);
  59. if(isset($path)){
  60.     usort($path, "SortByElementCount");
  61.     var_dump($path);   
  62. }
  63. if(!empty($PossiblePaths)){
  64.     usort($PossiblePaths, "SortByElementCount");
  65.     var_dump($PossiblePaths);
  66. }
  67. ?>
Advertisement
Add Comment
Please, Sign In to add comment