import "fmt"
import "json"
import "os"
+import "runtime/pprof"
import "strings"
var funds = flag.Int("funds", 0,
var visit_string = flag.String("visit", "",
"A comma-separated list of planets to make sure to visit")
+var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
+
+
+var visit_cache []string
func visit() []string {
- if *visit_string == "" {
- return nil
+ if visit_cache == nil {
+ if *visit_string == "" {
+ return nil
+ }
+ visit_cache = strings.Split(*visit_string, ",")
}
- return strings.Split(*visit_string, ",")
+ return visit_cache
}
+var flight_plan_cache []string
func flight_plan() []string {
- if *flight_plan_string == "" {
- return nil
+ if flight_plan_cache == nil {
+ if *flight_plan_string == "" {
+ return nil
+ }
+ flight_plan_cache = strings.Split(*flight_plan_string, ",")
}
- return strings.Split(*flight_plan_string, ",")
+ return flight_plan_cache
}
+var end_cache map[string]bool
func end() map[string]bool {
- if *end_string == "" {
- return nil
- }
- m := make(map[string]bool)
- for _, p := range strings.Split(*end_string, ",") {
- m[p] = true
+ if end_cache == nil {
+ if *end_string == "" {
+ return nil
+ }
+ m := make(map[string]bool)
+ for _, p := range strings.Split(*end_string, ",") {
+ m[p] = true
+ }
+ end_cache = m
}
- return m
+ return end_cache
}
type Commodity struct {
}
type Planet struct {
BeaconOn bool
+ Private bool
/* Use relative prices rather than absolute prices because you
can get relative prices without traveling to each planet. */
RelativePrices map[string]int
}
func FillCellBySelling(data planet_data, dims []int, table []State, addr []int) {
+ if data.Planets[data.i2p[addr[Location]]].Private {
+ // Can't do commerce on private planets
+ return
+ }
if addr[Hold] > 0 {
// Can't sell and still have cargo
return
}
func FillCellByBuying(data planet_data, dims []int, table []State, addr []int) {
+ if data.Planets[data.i2p[addr[Location]]].Private {
+ // Can't do commerce on private planets
+ return
+ }
if addr[Hold] == 0 {
// Can't buy and then have nothing
return
}
/* Visit this planet */
+ var i uint
+ for i = 0; i < uint(len(visit())); i++ {
+ if addr[Visit] & (1 << i) != 0 && visit()[i] == data.i2p[addr[Location]] {
+ other[Visit] = addr[Visit] & ^(1 << i)
+ UpdateCell(table, my_index, EncodeIndex(dims, other), 0)
+ }
+ }
+ other[Visit] = addr[Visit]
}
FillStateTable2Iteration(data, dims, table, addr, FillCellBySelling)
FillStateTable2Iteration(data, dims, table, addr, FillCellByBuying)
FillStateTable2Iteration(data, dims, table, addr, FillCellByMisc)
- barrier <- true
+ FillStateTable2Iteration(data, dims, table, addr, FillCellByBuyingEdens)
+ if barrier != nil {
+ barrier <- true
+ }
}
/* Filling the state table is a set of nested for loops NumDimensions deep.
work_units := (float64(*fuel) + 1) * (float64(eden_capacity) + 1)
work_done := 0.0
for fuel_remaining := *fuel; fuel_remaining >= 0; fuel_remaining-- {
+ /* Make an Eden-buying pass (Eden vendors' energy gradient
+ * along the Edens dimension runs backwards) */
+ for edens_remaining := 0; edens_remaining <= eden_capacity; edens_remaining++ {
+ for planet := range data.Planets {
+ if _, available := data.Planets[planet].RelativePrices["Eden Warp Units"]; available {
+ addr := make([]int, len(dims))
+ addr[Edens] = edens_remaining
+ addr[Fuel] = fuel_remaining
+ addr[Location] = data.p2i[planet]
+ FillStateTable2(data, dims, table, addr, nil)
+ }
+ }
+ }
for edens_remaining := eden_capacity; edens_remaining >= 0; edens_remaining-- {
/* Do the brunt of the work */
for planet := range data.Planets {
work_done++
print(fmt.Sprintf("\r%3.0f%%", 100*work_done/work_units))
}
- /* Make an Eden-buying pass (uphill) */
- addr := make([]int, len(dims))
- addr[Fuel] = fuel_remaining
- for addr[Edens] = 0; addr[Edens] <= eden_capacity; addr[Edens]++ {
- for planet := range data.Planets {
- addr[Location] = data.p2i[planet]
- FillStateTable2Iteration(data, dims, table, addr, FillCellByBuyingEdens)
- }
- }
}
print("\n")
}
addr[BuyFighters] = dims[BuyFighters] - 1
addr[BuyShields] = dims[BuyShields] - 1
addr[Visit] = dims[Visit] - 1
- // Fuel, Hold, UnusedCargo left at 0
+ // Hold and UnusedCargo left at 0
max_index := -1
max_value := 0
- for addr[Location] = 0; addr[Location] < dims[Location]; addr[Location]++ {
- if len(end()) == 0 || end()[data.i2p[addr[Location]]] {
- index := EncodeIndex(dims, addr)
- if table[index].value > max_value {
- max_value = table[index].value
- max_index = index
+ for addr[Fuel] = 0; addr[Fuel] < 2; addr[Fuel]++ {
+ for addr[Location] = 0; addr[Location] < dims[Location]; addr[Location]++ {
+ if len(end()) == 0 || end()[data.i2p[addr[Location]]] {
+ index := EncodeIndex(dims, addr)
+ if table[index].value > max_value {
+ max_value = table[index].value
+ max_index = index
+ }
}
}
}
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 == "" {
line = fmt.Sprint(prev, " -> ", addr)
}
func main() {
flag.Parse()
+ if *cpuprofile != "" {
+ f, err := os.Create(*cpuprofile)
+ if err != nil {
+ panic(err)
+ }
+ pprof.StartCPUProfile(f)
+ defer pprof.StopCPUProfile()
+ }
data := ReadData()
if *drone_price > 0 {
temp := data.Commodities["Fighter Drones"]