]> git.scottworley.com Git - planeteer/blame - planeteer.go
Consistent hyper-jump terminology.
[planeteer] / planeteer.go
CommitLineData
d07f3caa
SW
1/* Planeteer: Give trade route advice for Planets: The Exploration of Space
2 * Copyright (C) 2011 Scott Worley <sworley@chkno.net>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18package main
19
20import "flag"
c45c1bca 21import "fmt"
d07f3caa
SW
22import "json"
23import "os"
c45c1bca
SW
24import "strings"
25
330093c1
SW
26var funds = flag.Int("funds", 0,
27 "Starting funds")
28
c45c1bca
SW
29var start = flag.String("start", "",
30 "The planet to start at")
d07f3caa 31
e346cb37 32var flight_plan_string = flag.String("flight_plan", "",
63b4dbbc 33 "Your hyper-holes for the day, comma-separated.")
544108c4 34
1c1ede68 35var end_string = flag.String("end", "",
e9ff66cf 36 "A comma-separated list of acceptable ending planets.")
c45c1bca
SW
37
38var planet_data_file = flag.String("planet_data_file", "planet-data",
d07f3caa
SW
39 "The file to read planet data from")
40
63b4dbbc 41var fuel = flag.Int("fuel", 16, "Hyper Jump power left")
e9ff66cf
SW
42
43var hold = flag.Int("hold", 300, "Size of your cargo hold")
c45c1bca
SW
44
45var start_edens = flag.Int("start_edens", 0,
46 "How many Eden Warp Units are you starting with?")
47
48var end_edens = flag.Int("end_edens", 0,
49 "How many Eden Warp Units would you like to keep (not use)?")
50
51var cloak = flag.Bool("cloak", false,
52 "Make sure to end with a Device of Cloaking")
53
e9ff66cf 54var drones = flag.Int("drones", 0, "Buy this many Fighter Drones")
c45c1bca 55
e9ff66cf 56var batteries = flag.Int("batteries", 0, "Buy this many Shield Batterys")
c45c1bca
SW
57
58var visit_string = flag.String("visit", "",
59 "A comma-separated list of planets to make sure to visit")
60
61func visit() []string {
e346cb37 62 if *visit_string == "" {
1c1ede68 63 return nil
e346cb37 64 }
c45c1bca
SW
65 return strings.Split(*visit_string, ",")
66}
67
e346cb37
SW
68func flight_plan() []string {
69 if *flight_plan_string == "" {
1c1ede68 70 return nil
e346cb37
SW
71 }
72 return strings.Split(*flight_plan_string, ",")
73}
74
1c1ede68
SW
75func end() map[string]bool {
76 if *end_string == "" {
77 return nil
78 }
79 m := make(map[string]bool)
809e65f4 80 for _, p := range strings.Split(*end_string, ",") {
1c1ede68
SW
81 m[p] = true
82 }
83 return m
84}
85
9b3b3d9a 86type Commodity struct {
9b3b3d9a
SW
87 BasePrice int
88 CanSell bool
89 Limit int
90}
12bc2cd7 91type Planet struct {
12bc2cd7
SW
92 BeaconOn bool
93 /* Use relative prices rather than absolute prices because you
94 can get relative prices without traveling to each planet. */
0e94bdac 95 RelativePrices map[string]int
12bc2cd7 96}
d07f3caa 97type planet_data struct {
0e94bdac
SW
98 Commodities map[string]Commodity
99 Planets map[string]Planet
e7e4bc13
SW
100 p2i, c2i map[string]int // Generated; not read from file
101 i2p, i2c []string // Generated; not read from file
d07f3caa
SW
102}
103
104func ReadData() (data planet_data) {
c45c1bca 105 f, err := os.Open(*planet_data_file)
d07f3caa
SW
106 if err != nil {
107 panic(err)
108 }
109 defer f.Close()
110 err = json.NewDecoder(f).Decode(&data)
111 if err != nil {
112 panic(err)
113 }
114 return
115}
116
c45c1bca
SW
117/* This program operates by filling in a state table representing the best
118 * possible trips you could make; the ones that makes you the most money.
119 * This is feasible because we don't look at all the possible trips.
120 * We define a list of things that are germane to this game and then only
121 * consider the best outcome in each possible game state.
122 *
123 * Each cell in the table represents a state in the game. In each cell,
124 * we track two things: 1. the most money you could possibly have while in
125 * that state and 2. one possible way to get into that state with that
126 * amount of money.
127 *
128 * A basic analysis can be done with a two-dimensional table: location and
129 * fuel. planeteer-1.0 used this two-dimensional table. This version
130 * adds features mostly by adding dimensions to this table.
131 *
132 * Note that the sizes of each dimension are data driven. Many dimensions
133 * collapse to one possible value (ie, disappear) if the corresponding
134 * feature is not enabled.
e7e4bc13
SW
135 *
136 * The order of the dimensions in the list of constants below determines
137 * their layout in RAM. The cargo-based 'dimensions' are not completely
138 * independent -- some combinations are illegal and not used. They are
139 * handled as three dimensions rather than one for simplicity. Placing
140 * these dimensions first causes the unused cells in the table to be
141 * grouped together in large blocks. This keeps them from polluting
142 * cache lines, and if they are large enough, prevent the memory manager
143 * from allocating pages for these areas at all.
e346cb37
SW
144 *
145 * If the table gets too big to fit in RAM:
146 * * Combine the Edens, Cloaks, and UnusedCargo dimensions. Of the
147 * 24 combinations, only 15 are legal: a 38% savings.
148 * * Reduce the size of the Fuel dimension to 3. We only ever look
149 * backwards 2 units, so just rotate the logical values through
150 * the same 3 physical addresses. This is good for an 82% savings.
151 * * Reduce the size of the Edens dimension from 3 to 2, for the
152 * same reasons as Fuel above. 33% savings.
153 * * Buy more ram. (Just sayin'. It's cheaper than you think.)
154 *
c45c1bca
SW
155 */
156
157// The official list of dimensions:
158const (
e9ff66cf 159 // Name Num Size Description
0e94bdac
SW
160 Edens = iota // 1 3 # of Eden warp units (0 - 2 typically)
161 Cloaks // 2 2 # of Devices of Cloaking (0 or 1)
162 UnusedCargo // 3 4 # of unused cargo spaces (0 - 3 typically)
63b4dbbc 163 Fuel // 4 17 Hyper jump power left (0 - 16)
0e94bdac
SW
164 Location // 5 26 Location (which planet)
165 Hold // 6 15 Cargo bay contents (a *Commodity or nil)
166 NeedFighters // 7 2 Errand: Buy fighter drones (needed or not)
167 NeedShields // 8 2 Errand: Buy shield batteries (needed or not)
168 Visit // 9 2**N Visit: Stop by these N planets in the route
c45c1bca
SW
169
170 NumDimensions
171)
172
173func bint(b bool) int {
0e94bdac
SW
174 if b {
175 return 1
176 }
c45c1bca
SW
177 return 0
178}
179
180func DimensionSizes(data planet_data) []int {
181 eden_capacity := data.Commodities["Eden Warp Units"].Limit
330093c1
SW
182 if *start_edens > eden_capacity {
183 eden_capacity = *start_edens
184 }
c45c1bca 185 cloak_capacity := bint(*cloak)
64d87250
SW
186 dims := make([]int, NumDimensions)
187 dims[Edens] = eden_capacity + 1
188 dims[Cloaks] = cloak_capacity + 1
189 dims[UnusedCargo] = eden_capacity + cloak_capacity + 1
190 dims[Fuel] = *fuel + 1
191 dims[Location] = len(data.Planets)
c67c206a 192 dims[Hold] = len(data.Commodities) + 1
64d87250
SW
193 dims[NeedFighters] = bint(*drones > 0) + 1
194 dims[NeedShields] = bint(*batteries > 0) + 1
195 dims[Visit] = 1 << uint(len(visit()))
2f4ed5ca
SW
196
197 // Remind myself to add a line above when adding new dimensions
198 for i, dim := range dims {
199 if dim < 1 {
200 panic(i)
201 }
202 }
c45c1bca
SW
203 return dims
204}
205
206func StateTableSize(dims []int) int {
e346cb37 207 product := 1
c45c1bca 208 for _, size := range dims {
e346cb37 209 product *= size
c45c1bca 210 }
e346cb37 211 return product
c45c1bca
SW
212}
213
214type State struct {
544108c4 215 value, from int
c45c1bca
SW
216}
217
c45c1bca
SW
218func EncodeIndex(dims, addr []int) int {
219 index := addr[0]
e346cb37
SW
220 if addr[0] > dims[0] {
221 panic(0)
222 }
330093c1 223 for i := 1; i < NumDimensions; i++ {
d1ad6058 224 if addr[i] < 0 || addr[i] > dims[i] {
e346cb37
SW
225 panic(i)
226 }
0e94bdac 227 index = index*dims[i] + addr[i]
c45c1bca
SW
228 }
229 return index
230}
231
232func DecodeIndex(dims []int, index int) []int {
330093c1
SW
233 addr := make([]int, NumDimensions)
234 for i := NumDimensions - 1; i > 0; i-- {
c45c1bca
SW
235 addr[i] = index % dims[i]
236 index /= dims[i]
237 }
238 addr[0] = index
239 return addr
240}
241
330093c1
SW
242func InitializeStateTable(data planet_data, dims []int) []State {
243 table := make([]State, StateTableSize(dims))
244
245 addr := make([]int, NumDimensions)
246 addr[Fuel] = *fuel
247 addr[Edens] = *start_edens
248 addr[Location] = data.p2i[*start]
249 table[EncodeIndex(dims, addr)].value = *funds
250
251 return table
e346cb37
SW
252}
253
330093c1
SW
254/* These four fill procedures fill in the cell at address addr by
255 * looking at all the possible ways to reach this cell and selecting
256 * the best one.
544108c4
SW
257 *
258 * The other obvious implementation choice is to do this the other way
259 * around -- for each cell, conditionally overwrite all the other cells
260 * that are reachable *from* the considered cell. We choose gathering
261 * reads over scattering writes to avoid having to take a bunch of locks.
544108c4 262 */
330093c1
SW
263
264func UpdateCell(table []State, here, there, value_difference int) {
265 possible_value := table[there].value + value_difference
266 if table[there].value > 0 && possible_value > table[here].value {
267 table[here].value = possible_value
268 table[here].from = there
269 }
270}
271
272func FillCellByArriving(data planet_data, dims []int, table []State, addr []int) {
e346cb37
SW
273 my_index := EncodeIndex(dims, addr)
274 other := make([]int, NumDimensions)
275 copy(other, addr)
276
277 /* Travel here via a 2-fuel unit jump */
330093c1 278 if addr[Fuel]+2 < dims[Fuel] {
e346cb37 279 other[Fuel] = addr[Fuel] + 2
330093c1 280 for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ {
beb45aca
SW
281 if data.Planets[data.i2p[addr[Location]]].BeaconOn {
282 UpdateCell(table, my_index, EncodeIndex(dims, other), 0)
283 }
e346cb37
SW
284 }
285 other[Location] = addr[Location]
286 other[Fuel] = addr[Fuel]
287 }
288
63b4dbbc 289 /* Travel here via a hyper hole */
330093c1 290 if addr[Fuel]+1 < dims[Fuel] {
e346cb37 291 hole_index := (dims[Fuel] - 1) - (addr[Fuel] + 1)
7b5d9d13 292 if hole_index < len(flight_plan()) && addr[Location] == data.p2i[flight_plan()[hole_index]] {
e346cb37 293 other[Fuel] = addr[Fuel] + 1
7b5d9d13
SW
294 for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ {
295 UpdateCell(table, my_index, EncodeIndex(dims, other), 0)
296 }
330093c1 297 other[Location] = addr[Location]
e346cb37
SW
298 other[Fuel] = addr[Fuel]
299 }
300 }
301
544108c4 302 /* Travel here via Eden Warp Unit */
d1ad6058 303 if addr[Edens]+1 < dims[Edens] && addr[UnusedCargo] > 1 {
0c27c344
SW
304 _, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Eden Warp Units"]
305 if !available {
306 other[Edens] = addr[Edens] + 1
d1ad6058 307 other[UnusedCargo] = addr[UnusedCargo] - 1
0c27c344
SW
308 for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ {
309 UpdateCell(table, my_index, EncodeIndex(dims, other), 0)
310 }
311 other[Location] = addr[Location]
d1ad6058 312 other[UnusedCargo] = addr[UnusedCargo]
0c27c344 313 other[Edens] = addr[Edens]
330093c1
SW
314 }
315 }
330093c1
SW
316}
317
318func FillCellBySelling(data planet_data, dims []int, table []State, addr []int) {
319 if addr[Hold] > 0 {
320 // Can't sell and still have cargo
321 return
322 }
323 if addr[UnusedCargo] > 0 {
324 // Can't sell everything and still have 'unused' holds
325 return
326 }
327 my_index := EncodeIndex(dims, addr)
328 other := make([]int, NumDimensions)
329 copy(other, addr)
330 planet := data.i2p[addr[Location]]
331 for other[Hold] = 0; other[Hold] < dims[Hold]; other[Hold]++ {
332 commodity := data.i2c[other[Hold]]
333 if !data.Commodities[commodity].CanSell {
334 // TODO: Dump cargo
335 continue
336 }
337 relative_price, available := data.Planets[planet].RelativePrices[commodity]
338 if !available {
339 continue
340 }
341 base_price := data.Commodities[commodity].BasePrice
cbf01f59
SW
342 absolute_price := float64(base_price) * float64(relative_price) / 100.0
343 sell_price := int(absolute_price * 0.9)
330093c1
SW
344
345 for other[UnusedCargo] = 0; other[UnusedCargo] < dims[UnusedCargo]; other[UnusedCargo]++ {
346
ada59973 347 quantity := *hold - (other[UnusedCargo] + other[Cloaks] + other[Edens])
330093c1
SW
348 sale_value := quantity * sell_price
349 UpdateCell(table, my_index, EncodeIndex(dims, other), sale_value)
350 }
351 }
352 other[UnusedCargo] = addr[UnusedCargo]
353}
354
355func FillCellByBuying(data planet_data, dims []int, table []State, addr []int) {
356 if addr[Hold] == 0 {
357 // Can't buy and then have nothing
358 return
359 }
360 my_index := EncodeIndex(dims, addr)
361 other := make([]int, NumDimensions)
362 copy(other, addr)
363 planet := data.i2p[addr[Location]]
364 commodity := data.i2c[addr[Hold]]
365 if !data.Commodities[commodity].CanSell {
366 return
367 }
368 relative_price, available := data.Planets[planet].RelativePrices[commodity]
369 if !available {
370 return
371 }
372 base_price := data.Commodities[commodity].BasePrice
cbf01f59 373 absolute_price := int(float64(base_price) * float64(relative_price) / 100.0)
ada59973 374 quantity := *hold - (addr[UnusedCargo] + addr[Cloaks] + addr[Edens])
330093c1
SW
375 total_price := quantity * absolute_price
376 other[Hold] = 0
797391f8 377 other[UnusedCargo] = 0
330093c1 378 UpdateCell(table, my_index, EncodeIndex(dims, other), -total_price)
797391f8
SW
379 other[UnusedCargo] = addr[UnusedCargo]
380 other[Hold] = addr[Hold]
330093c1
SW
381}
382
383func FillCellByMisc(data planet_data, dims []int, table []State, addr []int) {
ada59973
SW
384 my_index := EncodeIndex(dims, addr)
385 other := make([]int, NumDimensions)
386 copy(other, addr)
d16f3322 387
544108c4 388 /* Buy a Device of Cloaking */
ada59973
SW
389 if addr[Cloaks] == 1 && addr[UnusedCargo] < dims[UnusedCargo]-1 {
390 relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Device Of Cloakings"]
391 if available {
392 absolute_price := int(float64(data.Commodities["Device Of Cloakings"].BasePrice) * float64(relative_price) / 100.0)
393 other[Cloaks] = 0
f800f732
SW
394 if other[Hold] != 0 {
395 other[UnusedCargo] = addr[UnusedCargo] + 1
396 }
ada59973
SW
397 UpdateCell(table, my_index, EncodeIndex(dims, other), -absolute_price)
398 other[UnusedCargo] = addr[UnusedCargo]
399 other[Cloaks] = addr[Cloaks]
400 }
401 }
544108c4
SW
402 /* Silly: Dump a Device of Cloaking */
403 /* Buy Fighter Drones */
404 /* Buy Shield Batteries */
544108c4 405 /* Visit this planet */
e7e4bc13
SW
406}
407
d16f3322
SW
408func FillCellByBuyingEdens(data planet_data, dims []int, table []State, addr []int) {
409 my_index := EncodeIndex(dims, addr)
410 other := make([]int, NumDimensions)
411 copy(other, addr)
412
413 /* Buy Eden warp units */
414 eden_limit := data.Commodities["Eden Warp Units"].Limit
415 if addr[Edens] > 0 && addr[Edens] <= eden_limit {
416 relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Eden Warp Units"]
417 if available {
418 absolute_price := int(float64(data.Commodities["Eden Warp Units"].BasePrice) * float64(relative_price) / 100.0)
419 for quantity := addr[Edens]; quantity > 0; quantity-- {
420 other[Edens] = addr[Edens] - quantity
421 if addr[Hold] != 0 {
422 other[UnusedCargo] = addr[UnusedCargo] + quantity
423 }
424 if other[UnusedCargo] < dims[UnusedCargo] {
425 UpdateCell(table, my_index, EncodeIndex(dims, other), -absolute_price * quantity)
426 }
427 }
428 other[Edens] = addr[Edens]
429 other[UnusedCargo] = addr[UnusedCargo]
430 }
431 }
432}
433
330093c1
SW
434func FillStateTable2Iteration(data planet_data, dims []int, table []State,
435addr []int, f func(planet_data, []int, []State, []int)) {
436 /* TODO: Justify the safety of the combination of this dimension
437 * iteration and the various phases f. */
e7e4bc13
SW
438 for addr[Hold] = 0; addr[Hold] < dims[Hold]; addr[Hold]++ {
439 for addr[Cloaks] = 0; addr[Cloaks] < dims[Cloaks]; addr[Cloaks]++ {
a1f10151 440 for addr[UnusedCargo] = 0; addr[UnusedCargo] < dims[UnusedCargo]; addr[UnusedCargo]++ {
330093c1
SW
441 for addr[NeedFighters] = 0; addr[NeedFighters] < dims[NeedFighters]; addr[NeedFighters]++ {
442 for addr[NeedShields] = 0; addr[NeedShields] < dims[NeedShields]; addr[NeedShields]++ {
443 for addr[Visit] = 0; addr[Visit] < dims[Visit]; addr[Visit]++ {
444 f(data, dims, table, addr)
e7e4bc13
SW
445 }
446 }
447 }
448 }
449 }
450 }
330093c1
SW
451}
452
453func FillStateTable2(data planet_data, dims []int, table []State,
d16f3322 454addr []int, barrier chan<- bool) {
330093c1
SW
455 FillStateTable2Iteration(data, dims, table, addr, FillCellByArriving)
456 FillStateTable2Iteration(data, dims, table, addr, FillCellBySelling)
457 FillStateTable2Iteration(data, dims, table, addr, FillCellByBuying)
458 FillStateTable2Iteration(data, dims, table, addr, FillCellByMisc)
e7e4bc13
SW
459 barrier <- true
460}
461
462/* Filling the state table is a set of nested for loops NumDimensions deep.
463 * We split this into two procedures: 1 and 2. #1 is the outer, slowest-
464 * changing indexes. #1 fires off many calls to #2 that run in parallel.
465 * The order of the nesting of the dimensions, the order of iteration within
466 * each dimension, and where the 1 / 2 split is placed are carefully chosen
467 * to make this arrangement safe.
468 *
469 * Outermost two layers: Go from high-energy states (lots of fuel, edens) to
470 * low-energy state. These must be processed sequentially and in this order
471 * because you travel through high-energy states to get to the low-energy
472 * states.
473 *
474 * Third layer: Planet. This is a good layer to parallelize on. There's
475 * high enough cardinality that we don't have to mess with parallelizing
476 * multiple layers for good utilization (on 2011 machines). Each thread
477 * works on one planet's states and need not synchronize with peer threads.
478 */
e346cb37 479func FillStateTable1(data planet_data, dims []int, table []State) {
e7e4bc13
SW
480 barrier := make(chan bool, len(data.Planets))
481 eden_capacity := data.Commodities["Eden Warp Units"].Limit
482 work_units := (float64(*fuel) + 1) * (float64(eden_capacity) + 1)
483 work_done := 0.0
484 for fuel_remaining := *fuel; fuel_remaining >= 0; fuel_remaining-- {
a1f10151 485 for edens_remaining := eden_capacity; edens_remaining >= 0; edens_remaining-- {
d16f3322 486 /* Do the brunt of the work */
e7e4bc13 487 for planet := range data.Planets {
d16f3322
SW
488 addr := make([]int, len(dims))
489 addr[Edens] = edens_remaining
490 addr[Fuel] = fuel_remaining
491 addr[Location] = data.p2i[planet]
492 go FillStateTable2(data, dims, table, addr, barrier)
e7e4bc13
SW
493 }
494 for _ = range data.Planets {
495 <-barrier
496 }
497 work_done++
9eafb7a4 498 print(fmt.Sprintf("\r%3.0f%%", 100*work_done/work_units))
e7e4bc13 499 }
d16f3322
SW
500 /* Make an Eden-buying pass (uphill) */
501 addr := make([]int, len(dims))
502 addr[Fuel] = fuel_remaining
503 for addr[Edens] = 0; addr[Edens] <= eden_capacity; addr[Edens]++ {
504 for planet := range data.Planets {
505 addr[Location] = data.p2i[planet]
506 FillStateTable2Iteration(data, dims, table, addr, FillCellByBuyingEdens)
507 }
508 }
e7e4bc13 509 }
e346cb37 510 print("\n")
e7e4bc13
SW
511}
512
ad4de13f
SW
513func FindBestState(data planet_data, dims []int, table []State) int {
514 addr := make([]int, NumDimensions)
515 addr[Edens] = *end_edens
516 addr[Cloaks] = dims[Cloaks] - 1
517 addr[NeedFighters] = dims[NeedFighters] - 1
518 addr[NeedShields] = dims[NeedShields] - 1
519 addr[Visit] = dims[Visit] - 1
520 // Fuel, Hold, UnusedCargo left at 0
ada59973 521 max_index := -1
ad4de13f
SW
522 max_value := 0
523 for addr[Location] = 0; addr[Location] < dims[Location]; addr[Location]++ {
809e65f4
SW
524 if len(end()) == 0 || end()[data.i2p[addr[Location]]] {
525 index := EncodeIndex(dims, addr)
526 if table[index].value > max_value {
527 max_value = table[index].value
528 max_index = index
529 }
ad4de13f
SW
530 }
531 }
532 return max_index
533}
534
9eafb7a4
SW
535func Commas(n int) (s string) {
536 r := n % 1000
537 n /= 1000
538 for n > 0 {
539 s = fmt.Sprintf(",%03d", r) + s
540 r = n % 1000
541 n /= 1000
542 }
543 s = fmt.Sprint(r) + s
544 return
545}
546
2f4a9ae8
SW
547func DescribePath(data planet_data, dims []int, table []State, start int) (description []string) {
548 for index := start; index > 0 && table[index].from > 0; index = table[index].from {
e4a1b48f 549 var line string
2f4a9ae8
SW
550 addr := DecodeIndex(dims, index)
551 prev := DecodeIndex(dims, table[index].from)
e4a1b48f 552 if addr[Fuel] != prev[Fuel] {
2f4a9ae8
SW
553 from := data.i2p[prev[Location]]
554 to := data.i2p[addr[Location]]
63b4dbbc 555 line += fmt.Sprintf("Jump from %v to %v (%v hyper jump units)", from, to, prev[Fuel]-addr[Fuel])
e4a1b48f 556 }
d16f3322 557 if addr[Edens] == prev[Edens] - 1 {
e4a1b48f
SW
558 from := data.i2p[prev[Location]]
559 to := data.i2p[addr[Location]]
560 line += fmt.Sprintf("Eden warp from %v to %v", from, to)
2f4a9ae8
SW
561 }
562 if addr[Hold] != prev[Hold] {
563 if addr[Hold] == 0 {
564 quantity := *hold - (prev[UnusedCargo] + prev[Edens] + prev[Cloaks])
e4a1b48f 565 line += fmt.Sprintf("Sell %v %v", quantity, data.i2c[prev[Hold]])
2f4a9ae8
SW
566 } else if prev[Hold] == 0 {
567 quantity := *hold - (addr[UnusedCargo] + addr[Edens] + addr[Cloaks])
e4a1b48f 568 line += fmt.Sprintf("Buy %v %v", quantity, data.i2c[addr[Hold]])
2f4a9ae8
SW
569 } else {
570 panic("Switched cargo?")
571 }
572
573 }
f800f732
SW
574 if addr[Cloaks] == 1 && prev[Cloaks] == 0 {
575 // TODO: Dump cloaks, convert from cargo?
e4a1b48f
SW
576 line += "Buy a Cloak"
577 }
d16f3322 578 if addr[Edens] > prev[Edens] {
e4a1b48f
SW
579 line += fmt.Sprint("Buy ", addr[Edens] - prev[Edens], " Eden Warp Units")
580 }
581 if line == "" {
582 line = fmt.Sprint(prev, " -> ", addr)
f800f732 583 }
e4a1b48f 584 description = append(description, fmt.Sprintf("%13v ", Commas(table[index].value)) + line)
2f4a9ae8
SW
585 }
586 return
587}
588
c45c1bca 589// (Example of a use case for generics in Go)
e7e4bc13 590func IndexPlanets(m *map[string]Planet, start_at int) (map[string]int, []string) {
a1f10151
SW
591 e2i := make(map[string]int, len(*m)+start_at)
592 i2e := make([]string, len(*m)+start_at)
e7e4bc13 593 i := start_at
c45c1bca 594 for e := range *m {
e7e4bc13
SW
595 e2i[e] = i
596 i2e[i] = e
c45c1bca
SW
597 i++
598 }
e7e4bc13 599 return e2i, i2e
c45c1bca 600}
e7e4bc13 601func IndexCommodities(m *map[string]Commodity, start_at int) (map[string]int, []string) {
a1f10151
SW
602 e2i := make(map[string]int, len(*m)+start_at)
603 i2e := make([]string, len(*m)+start_at)
e7e4bc13 604 i := start_at
c45c1bca 605 for e := range *m {
e7e4bc13
SW
606 e2i[e] = i
607 i2e[i] = e
c45c1bca
SW
608 i++
609 }
e7e4bc13 610 return e2i, i2e
c45c1bca
SW
611}
612
d07f3caa
SW
613func main() {
614 flag.Parse()
615 data := ReadData()
e7e4bc13
SW
616 data.p2i, data.i2p = IndexPlanets(&data.Planets, 0)
617 data.c2i, data.i2c = IndexCommodities(&data.Commodities, 1)
c45c1bca 618 dims := DimensionSizes(data)
330093c1 619 table := InitializeStateTable(data, dims)
e346cb37 620 FillStateTable1(data, dims, table)
ad4de13f 621 best := FindBestState(data, dims, table)
ada59973
SW
622 if best == -1 {
623 print("Cannot acheive success criteria\n")
624 } else {
ada59973
SW
625 description := DescribePath(data, dims, table, best)
626 for i := len(description) - 1; i >= 0; i-- {
627 fmt.Println(description[i])
628 }
2f4a9ae8 629 }
d07f3caa 630}