chiark / gitweb /
Accumulate totals and print running totals down the side of the route.
authormdw <mdw>
Tue, 4 Mar 2003 10:26:18 +0000 (10:26 +0000)
committermdw <mdw>
Tue, 4 Mar 2003 10:26:18 +0000 (10:26 +0000)
elite-path

index c5891aa008889c7967fca35fc8af940723748791..bcddd6b85fcb25c779e9bd1fa54d193a28319bb5 100755 (executable)
@@ -1,12 +1,13 @@
 #! /usr/bin/tclsh
 #
-# $Id: elite-path,v 1.2 2003/02/25 00:25:38 mdw Exp $
+# $Id: elite-path,v 1.3 2003/03/04 10:26:18 mdw Exp $
 
 package require "elite" "1.0.0"
 
 set g $galaxy1
 set ng 1
 set weight weight-hops
+set acc distance
 for {set i 0} {$i < [llength $argv]} {incr i} {
   set a [lindex $argv $i]
   switch -glob -- $a {
@@ -30,6 +31,17 @@ for {set i 0} {$i < [llength $argv]} {incr i} {
        exit 1
       }
     }
+    "-a" {
+      incr i
+      set a [lindex $argv $i]
+      switch -exact -- $a {
+       "none" - "distance" - "weight" { set acc $a }
+       default {
+         puts stderr "$argv0: unknown accumulation `$a'"
+         puts stderr "$argv0: I know `none', `distance' and `weight'"
+       }
+      }
+    }
     "--" {
       incr i
       break
@@ -62,27 +74,68 @@ foreach a [lrange $argv $i end] {
   lappend r $s
 }
 if {[llength $r] < 2} {
-  puts stderr "usage: $argv0 \[-g GALAXY\] \[-w WEIGHT\] PLANET PLANET ..."
+  puts stderr "usage: $argv0 \[-g GALAXY\] \[-w WEIGHT\] \[-a ACC\] PLANET PLANET ..."
   exit 1
 }
 puts -nonewline stderr "\[computing adjacency table..."
 adjacency $ww adj
 puts stderr " done\]"
 set home [lindex $r 0]
+set start $home
 set rt {}
+set tm 0
 foreach w [lrange $r 1 end] {
-  destructure {p .} [shortest-path adj $home $w $weight]
+  destructure {p m} [shortest-path adj $home $w $weight]
   if {![llength $p]} {
     puts -stderr "$argv0: no route from [worldinfo $home] to [worldinfo $w]"
     exit 1
   }
+  set tm [expr {$tm + $m}]
   eval lappend rt $p
   set home $w
 }
-set last x
+puts [format "  1 %s" [world-summary $start]]
+set last $start
+unset p
+elite-worldinfo p $start
+destructure {x y} [list $p(x) $p(y)]
+set h 1
+set td 0
+set tw 0
 foreach s $rt {
   if {![string equal $s $last]} {
-    puts [world-summary $s]
+    elite-worldinfo p $s
+    set d [expr {[world-distance $x $y $p(x) $p(y)]/10.0}]
+    incr h
+    set td [expr {$td + $d}]
+    set summ [format "%3d %s" $h [world-summary $s]]
+    set w [eval $weight [list $last $s]]
+    set tw [expr {$tw + $w}]
+    switch -- $acc {
+      "none" { }
+      "distance" {
+       append summ [format " (+ %.1f = %.1f LY)" $d $td]
+      }
+      "weight" {
+       append summ [format " (+ %s = %s)" $w $tw]
+      }
+    }
+    puts $summ
+    destructure {x y} [list $p(x) $p(y)]
     set last $s
   }
 }
+if {$tw != $tm} { error "inconsistent metric ($tw != $tm)" }
+set summ "("
+set sep ""
+if {![string equal $acc "distance"]} {
+  append summ [format "%stotal distance = %.1f LY" $sep $td]
+  set sep "; "
+}
+if {![string equal $weight "weight-hops"] && \
+    ![string equal $weight "weight-fuel"] && \
+    ![string equal $acc "weight"]} {
+  append summ [format "%stotal metric = %s" $sep $tm]
+}
+append summ ")"
+if {![string equal $summ "()"]} { puts $summ }