+ return max_index
+}
+
+func Commas(n Value) (s string) {
+ if n < 0 {
+ panic(n)
+ }
+ r := n % 1000
+ n /= 1000
+ for n > 0 {
+ s = fmt.Sprintf(",%03d", r) + s
+ r = n % 1000
+ n /= 1000
+ }
+ s = fmt.Sprint(r) + s
+ return
+}
+
+func FighterAndShieldCost(data planet_data, dims LogicalIndex, table []State, best PhysicalIndex) {
+ if *drones == 0 && *batteries == 0 {
+ return
+ }
+ fmt.Println()
+ if *drones > 0 {
+ final_state := FinalState(dims)
+ final_state[BuyFighters] = 0
+ alt_best := FindBestState(data, dims, table, final_state)
+ cost := table[alt_best].value - table[best].value
+ fmt.Println("\rDrones were", float64(cost)/float64(*drones), "each")
+ }
+ if *batteries > 0 {
+ final_state := FinalState(dims)
+ final_state[BuyShields] = 0
+ alt_best := FindBestState(data, dims, table, final_state)
+ cost := table[alt_best].value - table[best].value
+ fmt.Println("\rBatteries were", float64(cost)/float64(*batteries), "each")
+ }
+}
+
+func EndEdensCost(data planet_data, dims LogicalIndex, table []State, best PhysicalIndex) {
+ if *end_edens == 0 {
+ return
+ }
+ fmt.Println()
+ final_state := FinalState(dims)
+ for extra_edens := 1; extra_edens <= *end_edens; extra_edens++ {
+ final_state[Edens] = *end_edens - extra_edens
+ alt_best := FindBestState(data, dims, table, final_state)
+ extra_funds := table[alt_best].value - table[best].value
+ fmt.Println("\rUse", extra_edens, "extra edens, make an extra",
+ Commas(extra_funds), "(",
+ Commas(extra_funds/Value(extra_edens)), "per eden)")
+ }
+}
+
+func VisitCost(data planet_data, dims LogicalIndex, table []State, best PhysicalIndex) {
+ if dims[Visit] == 1 {
+ return
+ }
+ fmt.Println()
+ final_state := FinalState(dims)
+ for i := uint(0); i < uint(len(visit())); i++ {
+ all_bits := dims[Visit] - 1
+ final_state[Visit] = all_bits & ^(1 << i)
+ alt_best := FindBestState(data, dims, table, final_state)
+ cost := table[alt_best].value - table[best].value
+ fmt.Printf("\r%11v Cost to visit %v\n", Commas(cost), visit()[i])
+ }
+}
+
+func EndLocationCost(data planet_data, dims LogicalIndex, table []State, best PhysicalIndex) {
+ if len(end()) == 0 {
+ return
+ }
+ fmt.Println()
+ final_state := FinalState(dims)
+ save_end_string := *end_string
+ *end_string = ""
+ end_cache = nil
+ alt_best := FindBestState(data, dims, table, final_state)
+ cost := table[alt_best].value - table[best].value
+ fmt.Printf("\r%11v Cost of --end %v\n", Commas(cost), save_end_string)
+ *end_string = save_end_string
+}
+
+func DescribePath(data planet_data, dims LogicalIndex, table []State, start PhysicalIndex) (description []string) {
+ for index := start; table[index].from > FROM_ROOT; index = table[index].from {
+ if table[index].from == FROM_UNINITIALIZED {
+ panic(index)
+ }
+ var line string
+ addr := DecodeIndex(dims, index)
+ prev := DecodeIndex(dims, table[index].from)
+ if addr[Fuel] != prev[Fuel] {
+ from := data.i2p[prev[Location]]
+ to := data.i2p[addr[Location]]
+ line += fmt.Sprintf("Jump from %v to %v (%v hyper jump units)", from, to, prev[Fuel]-addr[Fuel])
+ }
+ if addr[Edens] == prev[Edens]-1 {
+ from := data.i2p[prev[Location]]
+ to := data.i2p[addr[Location]]
+ line += fmt.Sprintf("Eden warp from %v to %v", from, to)
+ }
+ if addr[Hold] != prev[Hold] {
+ if addr[Hold] == 0 {
+ quantity := *hold - (prev[UnusedCargo] + prev[Edens] + prev[Cloaks])
+ line += fmt.Sprintf("Sell %v %v", quantity, data.i2c[prev[Hold]])
+ } else if prev[Hold] == 0 {
+ quantity := *hold - (addr[UnusedCargo] + addr[Edens] + addr[Cloaks])
+ line += fmt.Sprintf("Buy %v %v", quantity, data.i2c[addr[Hold]])
+ } else {
+ panic("Switched cargo?")
+ }
+
+ }
+ if addr[Cloaks] == 1 && prev[Cloaks] == 0 {
+ // TODO: Dump cloaks, convert from cargo?
+ line += "Buy a Cloak"
+ }
+ if addr[Edens] > prev[Edens] {
+ line += fmt.Sprint("Buy ", addr[Edens]-prev[Edens], " Eden Warp Units")
+ }
+ if addr[BuyShields] == 1 && prev[BuyShields] == 0 {
+ line += fmt.Sprint("Buy ", *batteries, " Shield Batterys")
+ }
+ if addr[BuyFighters] == 1 && prev[BuyFighters] == 0 {
+ line += fmt.Sprint("Buy ", *drones, " Fighter Drones")
+ }
+ if addr[Visit] != prev[Visit] {
+ // TODO: verify that the bit chat changed is addr[Location]
+ line += fmt.Sprint("Visit ", data.i2p[addr[Location]])
+ }
+ if line == "" && addr[Hold] == prev[Hold] && addr[Traded] != prev[Traded] {
+ // The Traded dimension is for housekeeping. It doesn't directly
+ // correspond to in-game actions, so don't report transitions.
+ continue
+ }
+ if line == "" {
+ line = fmt.Sprint(prev, " -> ", addr)
+ }
+ description = append(description, fmt.Sprintf("%13v ", Commas(table[index].value))+line)
+ }
+ return