3 # $Id: elite.tcl,v 1.5 2003/03/04 10:26:47 mdw Exp $
5 package require "elite-bits" "1.0.0"
7 set galaxy1 "4a5a480253b7" ;# Seed for standard galaxy 1
9 # --- tab ARR NAME NAME ... ---
11 # Construct an array mapping integers 0, 1, ... to the given NAMEs, in order.
22 # --- Various standard tables ---
25 "anarchy" "feudal" "multi-government" "dictatorship" \
26 "communist" "confederacy" "democracy" "corporate state"
29 "rich industrial" "average industrial" "poor industrial" \
30 "mainly industrial" "mainly agricultural" "rich agricultural" \
31 "average agricultural" "poor agricultural"
34 anarchy feudal multi-gov dictator \
35 communist confed democracy corp-state
38 rich-ind avg-ind poor-ind mainly-ind \
39 mainly-agri rich-agri avg-agri poor-agri
44 radioactives "Radioactives"
46 liquor-wines "Liquor & wines"
57 gem-stones "Gem-stones"
58 alien-items "Alien items"
61 foreach p $products { set unit($p) t }
62 foreach p {gold platinum} { set unit($p) kg }
63 set unit(gem-stones) g
66 # --- galaxy N [GAL] ---
68 # Compute the seed of the Nth galaxy, if GAL is the seed of galaxy 1. By
69 # default, GAL is the standard galaxy 1 seed.
71 proc galaxy [list n [list g $galaxy1]] {
72 for {set i 1} {$i < $n} {incr i} {
73 set g [elite-nextgalaxy $g]
78 # --- foreach-world GAL ARR SCRIPT ---
80 # For each world in galaxy GAL (a seed), set ARR to the world information
81 # and evaluate SCRIPT. The usual loop control commands can be used in
84 proc foreach-world {g p act} {
86 for {set i 0} {$i < 256} {incr i; set g [elite-nextworld $g]} {
92 # --- find-world GAL PAT ---
94 # Return a list of seeds for the worlds in galaxy GAL (a seed) whose names
95 # match the glob pattern PAT.
97 proc find-world {g pat} {
100 if {[string match -nocase $pat $p(name)]} {
107 # --- destructure PAT LIST ---
109 # Destrcture LIST according to PAT. If PAT is a single name, set the
110 # variable PAT to LIST; otherwise, if PAT is a list, each of its elements
111 # must correspond to an element of LIST, so recursively destructure the
112 # corresponding elements of each. It is not an error if the PAT list is
113 # shorter than LIST. The special variable name `.' indicates that no
114 # assignment is to be made.
116 proc destructure {pp xx} {
117 if {![string compare $pp "."]} {
119 } elseif {[llength $pp] == 0} {
121 } elseif {[llength $pp] == 1} {
125 foreach p $pp x $xx {
126 uplevel 1 [list destructure $p $x]
131 # --- write-file NAME CONTENTS [TRANS] ---
133 # Write file NAME, storing CONTENTS translated according to TRANS (default
134 # `binary'. The write is safe against errors -- we don't destroy the old
135 # data until the file is written.
137 proc write-file {name contents {trans binary}} {
138 if {[file exists $name]} {
139 if {[set rc [catch { file copy -force $name "$name.old" } err]]} {
140 return -code $rc $err
145 fconfigure $f -translation $trans
146 puts -nonewline $f $contents
150 catch { file rename -force "$name.old" $name }
151 return -code $rc $err
156 # --- read-file NAME [TRANS] ---
158 # Read the contents of the file NAME, translating it according to TRANS
159 # (default `binary').
161 proc read-file {name {trans binary}} {
163 fconfigure $f -translation $trans
169 # --- worldinfo GAL ---
171 # Return a list describing the worlds in galaxy GAL (a seed). The list
172 # contains a group of three elements for each world: the seed, x and y
173 # coordinates (in decilightyears).
177 lappend i $p(seed) $p(x) $p(y)
182 # --- world-distance X Y XX YY ---
184 # Computes the correct game distance in decilightyears between two worlds,
185 # one at X, Y and the other at XX, YY.
187 proc world-distance {x y xx yy} {
188 set dx [expr {abs($x - $xx)/4}]
189 set dy [expr {abs($y - $yy)/4}]
190 return [expr {4 * floor(sqrt($dx * $dx + $dy * $dy))}]
193 # --- nearest-planet WW X Y ---
195 # Returns the seed of the `nearest' planet given in the worldinfo list WW to
196 # the point X Y (in decilightyears).
198 proc nearest-planet {ww x y} {
200 foreach {ss xx yy} $ww {
201 set dx [expr {abs($x - $xx)/4}]
202 set dy [expr {abs($y - $yy)/2}]
204 set d [expr {($dx * 2 + $dy)/2}]
206 set d [expr {($dx + $dy * 2)/2}]
216 # --- adjacency WW ADJ [D] ---
218 # Fill in the array ADJ with the adjacency table for the worlds listed in the
219 # worldinfo list WW. That is, for each world seed S, ADJ(S) is set to a
220 # worldinfo list containing the worlds within D (default 70) decilightyears
223 proc adjacency {p adj {d 70}} {
229 foreach {ss xx yy} $p {
230 if {[info exists done($ss)]} { continue }
231 if {abs($x - $xx) > $d + 10 || abs($y - $yy) > $d + 10 ||
232 [world-distance $x $y $xx $yy] > $d} { continue }
233 lappend a($s) $ss $xx $yy
234 lappend a($ss) $s $x $y
239 # --- worldname W ---
241 # Returns the name of the world with seed W.
248 # --- shortest-path ADJ FROM TO WEIGHT ---
250 # Computes the shortest path and shortest distance between the worlds wose
251 # seeds are FROM and TO respectively. ADJ must be an adjacency table for the
252 # galaxy containing FROM and TO. WEIGHT is a command such that WEIGHT A B
253 # returns the `distance' for the simple path between A and B. The return
254 # value is a list P D, where D is the weight of the path found, and P is a
255 # simple list of seeds for the worlds on the path. P starts with FROM and
258 proc shortest-path {adjvar from to weight} {
260 if {[string equal $from $to]} { return [list $to 0] }
265 foreach {n x y} $adj($c) {
266 if {[info exists l($n)]} {
269 set w [expr {$l($c) + [uplevel 1 $weight [list $c $n]]}]
270 if {![info exists ll($n)] || $w < $ll($n)} {
272 set p($n) [concat $p($c) [list $n]]
275 set s [array startsearch ll]
276 if {![array anymore ll $s]} {
279 set c [array nextelement ll $s]
281 while {[array anymore ll $s]} {
282 set n [array nextelement ll $s]
288 if {[string equal $c $to]} { return [list $p($to) $ll($to)] }
294 # --- weight-hops A B ---
296 # shortest-path weight function giving each hop the same weight.
298 proc weight-hops {from to} {
302 # --- weight-fuel A B ---
304 # shortest-path weight function measuring the distance between FROM and TO.
306 proc weight-fuel {from to} {
307 elite-worldinfo f $from
308 elite-worldinfo t $to
309 return [expr {[world-distance $f(x) $f(y) $t(x) $t(y)]/10.0}]
312 # --- weight-safety A B ---
314 # shortest-path weight function attempting to maximize safety of the journey
315 # by giving high weight (square-law) to worlds with unstable governments.
317 proc weight-safety {from to} {
318 elite-worldinfo t $to
319 set w [expr {8 - $t(government)}]
320 return [expr {$w * $w}]
323 # --- weight-encounters A B ---
325 # shortest-path weight function attempting to maximize encounters on the
326 # journey by giving high weight (square law) to worlds with stable
329 proc weight-encounters {from to} {
330 elite-worldinfo f $from
331 elite-worldinfo t $to
332 set w [expr {1 + $t(government)}]
333 return [expr {$w * $w}]
336 # --- weight-trading A B ---
338 # shortest-path weight function attempting to maximize trading opportunities
339 # along the journey by giving high weight (square law) to pairs of worlds
340 # with small differences between their economic statuses.
342 proc weight-trading {from to} {
343 elite-worldinfo f $from
344 elite-worldinfo t $to
345 set w [expr {8 - abs($f(economy) - $t(economy))}]
346 return [expr {$w * $w}]
349 # --- parse-galaxy-spec G ---
351 # Parses a galaxy spec and returns a list containing a description of the
352 # galaxy and the corresponding galaxy seed. A galaxy spec is one of:
354 # * a number between 1 and 8, corresponding to one of the standard
357 # * a 12-digit hex string, which is a galaxy seed (and is returned
360 # * a string of the form S:N where S is a 12-hex-digit seed and N is a
361 # galaxy number, corresponding to the Nth galaxy starting with S as
364 # If the string is unrecognized, an empty list is returned.
366 proc parse-galaxy-spec {g} {
367 switch -regexp -- $g {
368 {^[1-8]$} { return [list $g [galaxy $g]] }
369 {^[0-9a-fA-F]{12}$} { return [list $g $g] }
371 if {[regexp {^([0-9a-fA-F]{12}):([1-8])$} $g . b n]} {
372 return [list $g [galaxy $n $b]]
379 # --- parse-planet-spec G P ---
381 # Parses a planet spec and returns the planet seed. The planet spec P is
382 # interpreted relative to galaxy G. A planet spec is one of:
384 # * a simple integer, corresponding to a planet number;
386 # * a 12-hex-digit seed, which is returned unchanged;
388 # * a pair of integers separated by commas, corresponding to the nearest
389 # planet to those coordinates;
391 # * a glob pattern, corresponding to the lowest-numbered planet in the
392 # galaxy whose name matches the pattern case-insensitively; or
394 # * a string of the form G.P where G is a galaxy spec and P is a planet
395 # spec, corresponding to the planet specified by P relative to galaxy G.
397 # If the string is unrecognized, an empty string is returned.
399 proc parse-planet-spec {g p} {
400 if {[regexp {^[0-9a-fA-F]{12}$} $p]} { return $p }
401 if {[regexp {^(.+)\.(.+)$} $p . g p]} {
402 set g [parse-galaxy-spec $g]
403 if {[string equal $g ""]} { return {} }
405 return [parse-planet-spec $g $p]
407 if {[regexp {^(0x[0-9a-fA-F]+|[0-9]+)$} $p]} {
408 for {set s $g; set i 0} {$i < $p} {incr i; set s [elite-nextworld $s]} {}
411 if {[regexp {^(0x[0-9a-fA-F]+|[0-9]+),\s*(0x[0-9a-fA-F]+|[0-9]+)$} \
413 return [nearest-planet [worldinfo $g] $x $y]
415 set l [find-world $g $p]
416 if {[llength $l]} { return [lindex $l 0] }
420 # --- in-galaxy-p G PP ---
422 # Returns nonzero if the planets (seeds) listed in PP are in galaxy G.
423 # Doesn't mind if the planet seeds are invalid.
425 proc in-galaxy-p {g pp} {
426 foreach-world $g i { set x($i(seed)) 1 }
427 foreach p $pp { if {![info exists x($p)]} { return 0 } }
431 # --- world-summary PLANET ---
433 # Return a one-line summary string for PLANET.
435 proc world-summary {s} {
438 return [format "%-8s %4d %4d %-11s %-10s %2d %s" \
439 $p(name) $p(x) $p(y) \
440 $eco($p(economy)) $gov($p(government)) $p(techlevel) $p(seed)]
443 # --- jameson ARR ---
445 # Fill ARR with the information about commander JAMESON.
448 global galaxy1 products
465 set cmdr(gal-seed) $galaxy1
467 ecm fuel-scoop energy-bomb energy-unit docking-computer
468 gal-hyperdrive escape-pod
470 elite-worldinfo lave [find-world $galaxy1 "Lave"]
471 set cmdr(world-x) [expr {$lave(x)/4}]
472 set cmdr(world-y) [expr {$lave(y)/2}]
473 elite-market mkt $lave(seed) 0
474 foreach {t n} $products {
475 destructure [list . cmdr(station-$t)] $mkt($t)
478 set cmdr(station-alien-items) 0
481 #----- That's all, folks ----------------------------------------------------
483 package provide "elite" "1.0.0"