From 43c77c8fb92d3c4ff1f12d80509d15d67405751e Mon Sep 17 00:00:00 2001 From: mdw Date: Sun, 9 Mar 2003 23:45:02 +0000 Subject: [PATCH] Improve elite-prices a lot. --- README | 12 ++++++-- elite-prices | 91 +++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 59 insertions(+), 44 deletions(-) diff --git a/README b/README index ad122ab..30e32ff 100644 --- a/README +++ b/README @@ -371,7 +371,7 @@ RIGHT ON COMMAND-LINE `-load' option. - elite-prices [-g GALAXY] [-s SORT] [FROM TO] + elite-prices [-g GALAXY] [-s SORT] [FROM [TO]] Shows minimum, average and maximum profit (in that order, in credits per unit) for the various commodities, starting at one @@ -383,11 +383,17 @@ RIGHT ON COMMAND-LINE You can change the worlds under consideration by typing a pair of planet-specs or economy types (as printed by `elite-find'). - Any planet-specs are obviously taken relative to GALAXY. + Any planet-specs are obviously taken relative to GALAXY. You + can also say `avg' for the average over all economy types + (though this takes longer to compute). The SORT parameter may be one of `min', `max', or `avg' to sort by minimum, maximum or average profit (highest at the top). + If only FROM is given, the average prices for the given world or + economy type are printed in the standard order (i.e., the SORT + parameter is ignored). + elite-salesman [-OPTIONS] GALAXY [START] @@ -558,7 +564,7 @@ RIGHT ON COMMAND-LINE unrewarding) or pirates (risky and tedious), and start trading food and other cheap items. -$Id: README,v 1.8 2003/03/09 17:59:48 mdw Exp $ +$Id: README,v 1.9 2003/03/09 23:45:02 mdw Exp $ Local variables: mode: text diff --git a/elite-prices b/elite-prices index 07ad778..1af510a 100755 --- a/elite-prices +++ b/elite-prices @@ -1,14 +1,21 @@ #! /usr/bin/tclsh # -# $Id: elite-prices,v 1.4 2003/03/07 00:41:46 mdw Exp $ +# $Id: elite-prices,v 1.5 2003/03/09 23:45:02 mdw Exp $ package require "elite" "1.0.1" +set i 0 +set allreps {} foreach-world $galaxy1 p { - set econame $eco($p(economy)) - if {[info exists rep($econame)]} { continue } - set rep($econame) $p(seed) + set e $eco($p(economy)) + set s $p(seed) + if {[info exists rep($e)]} { continue } + set rep($e) $s + lappend allreps $s + incr i + if {$i == 8} { break } } +set rep(avg) "" unset p set from $rep(poor-agri) @@ -39,41 +46,22 @@ proc get-world {p} { return $s } -proc check-profits {from to} { - global products - set np [expr {[llength $products]/2}] - puts -nonewline stderr "Thinking: \[[string repeat { } 32]\] " - puts -nonewline stderr "\[[string repeat { } $np]\] " - puts -nonewline stderr "\rThinking: \[" - flush stderr - foreach {a s} [list l $from e $to] { - for {set f 0} {$f < 256} {incr f} { - elite-market m $s $f - foreach {t p} $products { destructure [list ${a}($f:$t) .] $m($t) } - if {($f & 15) == 15} { puts -nonewline stderr "."; flush stderr } +proc get-prices {ss arr} { + global products allreps + upvar \#0 $arr a + foreach {t p} $products { set l($t) {} } + foreach s [if {[string equal $ss ""]} { set allreps } { set ss }] { + if {![string equal $s ""]} { + for {set i 0} {$i < 256} {incr i} { + elite-market m $s $i + foreach {t p} $products { lappend l($t) [lindex $m($t) 0] } + } } } - puts -nonewline stderr "\] \[" - flush stderr - set r {} foreach {t p} $products { - set ll {} - set ee {} - for {set f 0} {$f < 256} {incr f} { - lappend ll $l($f:$t) - lappend ee $e($f:$t) - } - destructure {llo lavg lhi} [loavghi $ll] - destructure {elo eavg ehi} [loavghi $ee] - lappend r [list $t \ - [expr {$elo - $lhi}] \ - [expr {int($eavg - $lavg)}] \ - [expr {$ehi - $llo}]] - puts -nonewline stderr "."; flush stderr + set a($t) [loavghi $l($t)] } - puts stderr "\] done" - return $r -} +} set g $galaxy1 set sortcol 0 @@ -120,11 +108,21 @@ for {set i 0} {$i < [llength $argv]} {incr i} { set argv [lrange $argv $i end] switch -exact -- [llength $argv] { 0 { } + 1 { + set w [get-world [lindex $argv 0]] + get-prices $w pp + foreach {t p} $products { + destructure {min avg max} $pp($t) + puts [format "%-12s %5.1f %5.1f %5.1f" $t \ + [expr {$min/10.0}] [expr {$avg/10.0}] [expr {$max/10.0}]] + } + exit + } 2 { - destructure {fp tp} $argv - set from [get-world $fp] - set to [get-world $tp] - foreach {p s} [list $fp $from $tp $to] { + destructure {f t} $argv + set from [get-world $f] + set to [get-world $t] + foreach {p s} [list $f $from $t $to] { if {[string equal $s ""]} { puts stderr "$argv0: bad planet spec `$p'" exit 1 @@ -137,12 +135,23 @@ switch -exact -- [llength $argv] { } } -set pp [check-profits $from $to] +get-prices $from fp +get-prices $to tp +set pp {} +foreach {t p} $products { + destructure {flo favg fhi} $fp($t) + destructure {tlo tavg thi} $tp($t) + lappend pp [list $t \ + [expr {$tlo - $fhi}] \ + [expr {int($tavg - $favg)}] \ + [expr {$thi - $flo}]] +} + if {$sortcol} { set pp [lsort -index $sortcol -real -decreasing $pp] } foreach i $pp { destructure {t min avg max} $i - puts [format "%-15s %6.1f %5.1f %5.1f" $t \ + puts [format "%-12s %6.1f %5.1f %5.1f" $t \ [expr {$min/10.0}] [expr {$avg/10.0}] [expr {$max/10.0}]] } -- 2.11.0