]>
Commit | Line | Data |
---|---|---|
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 | ||
18 | package main | |
19 | ||
20 | import "flag" | |
21 | import "fmt" | |
22 | import "json" | |
23 | import "os" | |
24 | import "runtime/pprof" | |
25 | import "strings" | |
26 | ||
27 | var funds = flag.Int("funds", 0, | |
28 | "Starting funds") | |
29 | ||
30 | var start = flag.String("start", "", | |
31 | "The planet to start at") | |
32 | ||
33 | var flight_plan_string = flag.String("flight_plan", "", | |
34 | "Your hyper-holes for the day, comma-separated.") | |
35 | ||
36 | var end_string = flag.String("end", "", | |
37 | "A comma-separated list of acceptable ending planets.") | |
38 | ||
39 | var planet_data_file = flag.String("planet_data_file", "planet-data", | |
40 | "The file to read planet data from") | |
41 | ||
42 | var fuel = flag.Int("fuel", 16, "Hyper Jump power left") | |
43 | ||
44 | var hold = flag.Int("hold", 300, "Size of your cargo hold") | |
45 | ||
46 | var start_edens = flag.Int("start_edens", 0, | |
47 | "How many Eden Warp Units are you starting with?") | |
48 | ||
49 | var end_edens = flag.Int("end_edens", 0, | |
50 | "How many Eden Warp Units would you like to keep (not use)?") | |
51 | ||
52 | var cloak = flag.Bool("cloak", false, | |
53 | "Make sure to end with a Device of Cloaking") | |
54 | ||
55 | var drones = flag.Int("drones", 0, "Buy this many Fighter Drones") | |
56 | ||
57 | var batteries = flag.Int("batteries", 0, "Buy this many Shield Batterys") | |
58 | ||
59 | var drone_price = flag.Int("drone_price", 0, "Today's Fighter Drone price") | |
60 | ||
61 | var battery_price = flag.Int("battery_price", 0, "Today's Shield Battery price") | |
62 | ||
63 | var visit_string = flag.String("visit", "", | |
64 | "A comma-separated list of planets to make sure to visit") | |
65 | ||
66 | var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") | |
67 | ||
68 | ||
69 | var visit_cache []string | |
70 | func visit() []string { | |
71 | if visit_cache == nil { | |
72 | if *visit_string == "" { | |
73 | return nil | |
74 | } | |
75 | visit_cache = strings.Split(*visit_string, ",") | |
76 | } | |
77 | return visit_cache | |
78 | } | |
79 | ||
80 | var flight_plan_cache []string | |
81 | func flight_plan() []string { | |
82 | if flight_plan_cache == nil { | |
83 | if *flight_plan_string == "" { | |
84 | return nil | |
85 | } | |
86 | flight_plan_cache = strings.Split(*flight_plan_string, ",") | |
87 | } | |
88 | return flight_plan_cache | |
89 | } | |
90 | ||
91 | var end_cache map[string]bool | |
92 | func end() map[string]bool { | |
93 | if end_cache == nil { | |
94 | if *end_string == "" { | |
95 | return nil | |
96 | } | |
97 | m := make(map[string]bool) | |
98 | for _, p := range strings.Split(*end_string, ",") { | |
99 | m[p] = true | |
100 | } | |
101 | end_cache = m | |
102 | } | |
103 | return end_cache | |
104 | } | |
105 | ||
106 | type Commodity struct { | |
107 | BasePrice int | |
108 | CanSell bool | |
109 | Limit int | |
110 | } | |
111 | type Planet struct { | |
112 | BeaconOn bool | |
113 | Private bool | |
114 | /* Use relative prices rather than absolute prices because you | |
115 | can get relative prices without traveling to each planet. */ | |
116 | RelativePrices map[string]int | |
117 | } | |
118 | type planet_data struct { | |
119 | Commodities map[string]Commodity | |
120 | Planets map[string]Planet | |
121 | p2i, c2i map[string]int // Generated; not read from file | |
122 | i2p, i2c []string // Generated; not read from file | |
123 | } | |
124 | ||
125 | func ReadData() (data planet_data) { | |
126 | f, err := os.Open(*planet_data_file) | |
127 | if err != nil { | |
128 | panic(err) | |
129 | } | |
130 | defer f.Close() | |
131 | err = json.NewDecoder(f).Decode(&data) | |
132 | if err != nil { | |
133 | panic(err) | |
134 | } | |
135 | return | |
136 | } | |
137 | ||
138 | /* This program operates by filling in a state table representing the best | |
139 | * possible trips you could make; the ones that makes you the most money. | |
140 | * This is feasible because we don't look at all the possible trips. | |
141 | * We define a list of things that are germane to this game and then only | |
142 | * consider the best outcome in each possible game state. | |
143 | * | |
144 | * Each cell in the table represents a state in the game. In each cell, | |
145 | * we track two things: 1. the most money you could possibly have while in | |
146 | * that state and 2. one possible way to get into that state with that | |
147 | * amount of money. | |
148 | * | |
149 | * A basic analysis can be done with a two-dimensional table: location and | |
150 | * fuel. planeteer-1.0 used this two-dimensional table. This version | |
151 | * adds features mostly by adding dimensions to this table. | |
152 | * | |
153 | * Note that the sizes of each dimension are data driven. Many dimensions | |
154 | * collapse to one possible value (ie, disappear) if the corresponding | |
155 | * feature is not enabled. | |
156 | * | |
157 | * The order of the dimensions in the list of constants below determines | |
158 | * their layout in RAM. The cargo-based 'dimensions' are not completely | |
159 | * independent -- some combinations are illegal and not used. They are | |
160 | * handled as three dimensions rather than one for simplicity. Placing | |
161 | * these dimensions first causes the unused cells in the table to be | |
162 | * grouped together in large blocks. This keeps them from polluting | |
163 | * cache lines, and if they are large enough, allows the memory manager | |
164 | * to swap out entire pages. | |
165 | * | |
166 | * If the table gets too big to fit in RAM: | |
167 | * * Combine the Edens, Cloaks, and UnusedCargo dimensions. Of the | |
168 | * 24 combinations, only 15 are legal: a 38% savings. | |
169 | * * Reduce the size of the Fuel dimension to 3. We only ever look | |
170 | * backwards 2 units, so just rotate the logical values through | |
171 | * the same 3 physical addresses. This is good for an 82% savings. | |
172 | * * Reduce the size of the Edens dimension from 3 to 2, for the | |
173 | * same reasons as Fuel above. 33% savings. | |
174 | * * Buy more ram. (Just sayin'. It's cheaper than you think.) | |
175 | * | |
176 | */ | |
177 | ||
178 | // The official list of dimensions: | |
179 | const ( | |
180 | // Name Num Size Description | |
181 | Edens = iota // 1 3 # of Eden warp units (0 - 2 typically) | |
182 | Cloaks // 2 1-2 # of Devices of Cloaking (0 or 1) | |
183 | UnusedCargo // 3 4 # of unused cargo spaces (0 - 3 typically) | |
184 | Fuel // 4 17 Hyper jump power left (0 - 16) | |
185 | Location // 5 26 Location (which planet) | |
186 | Hold // 6 15 Cargo bay contents (a *Commodity or nil) | |
187 | Traded // 7 2 Traded yet? | |
188 | BuyFighters // 8 1-2 Errand: Buy fighter drones | |
189 | BuyShields // 9 1-2 Errand: Buy shield batteries | |
190 | Visit // 10 1-2**N Visit: Stop by these N planets in the route | |
191 | ||
192 | NumDimensions | |
193 | ) | |
194 | ||
195 | func bint(b bool) int { | |
196 | if b { | |
197 | return 1 | |
198 | } | |
199 | return 0 | |
200 | } | |
201 | ||
202 | func DimensionSizes(data planet_data) []int { | |
203 | eden_capacity := data.Commodities["Eden Warp Units"].Limit | |
204 | if *start_edens > eden_capacity { | |
205 | eden_capacity = *start_edens | |
206 | } | |
207 | cloak_capacity := bint(*cloak) | |
208 | dims := make([]int, NumDimensions) | |
209 | dims[Edens] = eden_capacity + 1 | |
210 | dims[Cloaks] = cloak_capacity + 1 | |
211 | dims[UnusedCargo] = eden_capacity + cloak_capacity + 1 | |
212 | dims[Fuel] = *fuel + 1 | |
213 | dims[Location] = len(data.Planets) | |
214 | dims[Hold] = len(data.Commodities) + 1 | |
215 | dims[Traded] = 2 | |
216 | dims[BuyFighters] = bint(*drones > 0) + 1 | |
217 | dims[BuyShields] = bint(*batteries > 0) + 1 | |
218 | dims[Visit] = 1 << uint(len(visit())) | |
219 | ||
220 | // Remind myself to add a line above when adding new dimensions | |
221 | for i, dim := range dims { | |
222 | if dim < 1 { | |
223 | panic(i) | |
224 | } | |
225 | } | |
226 | return dims | |
227 | } | |
228 | ||
229 | func StateTableSize(dims []int) int { | |
230 | product := 1 | |
231 | for _, size := range dims { | |
232 | product *= size | |
233 | } | |
234 | return product | |
235 | } | |
236 | ||
237 | type State struct { | |
238 | value, from int32 | |
239 | } | |
240 | ||
241 | const CELL_UNINITIALIZED = -2147483647 | |
242 | const CELL_BEING_EVALUATED = -2147483646 | |
243 | const CELL_RUBISH = -2147483645 | |
244 | ||
245 | func EncodeIndex(dims, addr []int) int32 { | |
246 | index := addr[0] | |
247 | if addr[0] > dims[0] { | |
248 | panic(0) | |
249 | } | |
250 | for i := 1; i < NumDimensions; i++ { | |
251 | if addr[i] < 0 || addr[i] >= dims[i] { | |
252 | panic(i) | |
253 | } | |
254 | index = index*dims[i] + addr[i] | |
255 | } | |
256 | return int32(index) | |
257 | } | |
258 | ||
259 | func DecodeIndex(dims []int, index int32) []int { | |
260 | addr := make([]int, NumDimensions) | |
261 | for i := NumDimensions - 1; i > 0; i-- { | |
262 | addr[i] = int(index) % dims[i] | |
263 | index /= int32(dims[i]) | |
264 | } | |
265 | addr[0] = int(index) | |
266 | return addr | |
267 | } | |
268 | ||
269 | func CreateStateTable(data planet_data, dims []int) []State { | |
270 | table := make([]State, StateTableSize(dims)) | |
271 | for i := range table { | |
272 | table[i].value = CELL_UNINITIALIZED | |
273 | } | |
274 | ||
275 | addr := make([]int, NumDimensions) | |
276 | addr[Fuel] = *fuel | |
277 | addr[Edens] = *start_edens | |
278 | addr[Location] = data.p2i[*start] | |
279 | addr[Traded] = 1 | |
280 | table[EncodeIndex(dims, addr)].value = int32(*funds) | |
281 | ||
282 | return table | |
283 | } | |
284 | ||
285 | /* CellValue fills in the one cell at address addr by looking at all | |
286 | * the possible ways to reach this cell and selecting the best one. */ | |
287 | ||
288 | func Consider(data planet_data, dims []int, table []State, there []int, value_difference int, best_value *int32, best_source []int) { | |
289 | there_value := CellValue(data, dims, table, there) | |
290 | if value_difference < 0 && int32(-value_difference) > there_value { | |
291 | /* Can't afford this transition */ | |
292 | return | |
293 | } | |
294 | possible_value := there_value + int32(value_difference) | |
295 | if possible_value > *best_value { | |
296 | *best_value = possible_value | |
297 | copy(best_source, there) | |
298 | } | |
299 | } | |
300 | ||
301 | var cell_filled_count int | |
302 | func CellValue(data planet_data, dims []int, table []State, addr []int) int32 { | |
303 | my_index := EncodeIndex(dims, addr) | |
304 | if table[my_index].value == CELL_BEING_EVALUATED { | |
305 | panic("Circular dependency") | |
306 | } | |
307 | if table[my_index].value != CELL_UNINITIALIZED { | |
308 | return table[my_index].value | |
309 | } | |
310 | table[my_index].value = CELL_BEING_EVALUATED | |
311 | ||
312 | best_value := int32(CELL_RUBISH) | |
313 | best_source := make([]int, NumDimensions) | |
314 | other := make([]int, NumDimensions) | |
315 | copy(other, addr) | |
316 | planet := data.i2p[addr[Location]] | |
317 | ||
318 | /* Travel here */ | |
319 | if addr[Traded] == 0 { /* Can't have traded immediately after traveling. */ | |
320 | other[Traded] = 1 /* Travel from states that have done trading. */ | |
321 | ||
322 | /* Travel here via a 2-fuel unit jump */ | |
323 | if addr[Fuel]+2 < dims[Fuel] { | |
324 | other[Fuel] = addr[Fuel] + 2 | |
325 | hole_index := (dims[Fuel] - 1) - (addr[Fuel] + 2) | |
326 | if hole_index >= len(flight_plan()) || addr[Location] != data.p2i[flight_plan()[hole_index]] { | |
327 | for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ { | |
328 | if data.Planets[data.i2p[addr[Location]]].BeaconOn { | |
329 | Consider(data, dims, table, other, 0, &best_value, best_source) | |
330 | } | |
331 | } | |
332 | } | |
333 | other[Location] = addr[Location] | |
334 | other[Fuel] = addr[Fuel] | |
335 | } | |
336 | ||
337 | /* Travel here via a 1-fuel unit jump (a hyper hole) */ | |
338 | if addr[Fuel]+1 < dims[Fuel] { | |
339 | hole_index := (dims[Fuel] - 1) - (addr[Fuel] + 1) | |
340 | if hole_index < len(flight_plan()) && addr[Location] == data.p2i[flight_plan()[hole_index]] { | |
341 | other[Fuel] = addr[Fuel] + 1 | |
342 | for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ { | |
343 | Consider(data, dims, table, other, 0, &best_value, best_source) | |
344 | } | |
345 | other[Location] = addr[Location] | |
346 | other[Fuel] = addr[Fuel] | |
347 | } | |
348 | } | |
349 | ||
350 | /* Travel here via Eden Warp Unit */ | |
351 | if addr[Edens]+1 < dims[Edens] && addr[UnusedCargo] > 1 { | |
352 | _, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Eden Warp Units"] | |
353 | if !available { | |
354 | other[Edens] = addr[Edens] + 1 | |
355 | if other[Hold] != 0 { | |
356 | other[UnusedCargo] = addr[UnusedCargo] - 1 | |
357 | } | |
358 | for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ { | |
359 | Consider(data, dims, table, other, 0, &best_value, best_source) | |
360 | } | |
361 | other[Location] = addr[Location] | |
362 | other[UnusedCargo] = addr[UnusedCargo] | |
363 | other[Edens] = addr[Edens] | |
364 | } | |
365 | } | |
366 | other[Traded] = addr[Traded] | |
367 | } | |
368 | ||
369 | /* Trade */ | |
370 | if addr[Traded] == 1 { | |
371 | other[Traded] = 0 | |
372 | ||
373 | /* Consider not trading */ | |
374 | Consider(data, dims, table, other, 0, &best_value, best_source) | |
375 | ||
376 | if !data.Planets[data.i2p[addr[Location]]].Private { | |
377 | ||
378 | /* Sell */ | |
379 | if addr[Hold] == 0 && addr[UnusedCargo] == 0 { | |
380 | for other[Hold] = 0; other[Hold] < dims[Hold]; other[Hold]++ { | |
381 | commodity := data.i2c[other[Hold]] | |
382 | if !data.Commodities[commodity].CanSell { | |
383 | continue | |
384 | } | |
385 | relative_price, available := data.Planets[planet].RelativePrices[commodity] | |
386 | if !available { | |
387 | // TODO: Dump cargo | |
388 | continue | |
389 | } | |
390 | base_price := data.Commodities[commodity].BasePrice | |
391 | absolute_price := float64(base_price) * float64(relative_price) / 100.0 | |
392 | sell_price := int(absolute_price * 0.9) | |
393 | ||
394 | for other[UnusedCargo] = 0; other[UnusedCargo] < dims[UnusedCargo]; other[UnusedCargo]++ { | |
395 | quantity := *hold - (other[UnusedCargo] + other[Cloaks] + other[Edens]) | |
396 | sale_value := quantity * sell_price | |
397 | Consider(data, dims, table, other, sale_value, &best_value, best_source) | |
398 | } | |
399 | } | |
400 | other[UnusedCargo] = addr[UnusedCargo] | |
401 | other[Hold] = addr[Hold] | |
402 | } | |
403 | ||
404 | /* Buy */ | |
405 | other[Traded] = addr[Traded] /* Buy after selling */ | |
406 | if addr[Hold] != 0 { | |
407 | commodity := data.i2c[addr[Hold]] | |
408 | if data.Commodities[commodity].CanSell { | |
409 | relative_price, available := data.Planets[planet].RelativePrices[commodity] | |
410 | if available { | |
411 | base_price := data.Commodities[commodity].BasePrice | |
412 | absolute_price := int(float64(base_price) * float64(relative_price) / 100.0) | |
413 | quantity := *hold - (addr[UnusedCargo] + addr[Cloaks] + addr[Edens]) | |
414 | total_price := quantity * absolute_price | |
415 | other[Hold] = 0 | |
416 | other[UnusedCargo] = 0 | |
417 | Consider(data, dims, table, other, -total_price, &best_value, best_source) | |
418 | other[UnusedCargo] = addr[UnusedCargo] | |
419 | other[Hold] = addr[Hold] | |
420 | } | |
421 | } | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
426 | /* Buy a Device of Cloaking */ | |
427 | if addr[Cloaks] == 1 && addr[UnusedCargo] < dims[UnusedCargo]-1 { | |
428 | relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Device Of Cloakings"] | |
429 | if available { | |
430 | absolute_price := int(float64(data.Commodities["Device Of Cloakings"].BasePrice) * float64(relative_price) / 100.0) | |
431 | other[Cloaks] = 0 | |
432 | if other[Hold] != 0 { | |
433 | other[UnusedCargo] = addr[UnusedCargo] + 1 | |
434 | } | |
435 | Consider(data, dims, table, other, -absolute_price, &best_value, best_source) | |
436 | other[UnusedCargo] = addr[UnusedCargo] | |
437 | other[Cloaks] = addr[Cloaks] | |
438 | } | |
439 | } | |
440 | ||
441 | /* Buy Fighter Drones */ | |
442 | if addr[BuyFighters] == 1 { | |
443 | relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Fighter Drones"] | |
444 | if available { | |
445 | absolute_price := int(float64(data.Commodities["Fighter Drones"].BasePrice) * float64(relative_price) / 100.0) | |
446 | other[BuyFighters] = 0 | |
447 | Consider(data, dims, table, other, -absolute_price * *drones, &best_value, best_source) | |
448 | other[BuyFighters] = addr[BuyFighters] | |
449 | } | |
450 | } | |
451 | ||
452 | /* Buy Shield Batteries */ | |
453 | if addr[BuyShields] == 1 { | |
454 | relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Shield Batterys"] | |
455 | if available { | |
456 | absolute_price := int(float64(data.Commodities["Shield Batterys"].BasePrice) * float64(relative_price) / 100.0) | |
457 | other[BuyShields] = 0 | |
458 | Consider(data, dims, table, other, -absolute_price * *batteries, &best_value, best_source) | |
459 | other[BuyShields] = addr[BuyShields] | |
460 | } | |
461 | } | |
462 | ||
463 | /* Visit this planet */ | |
464 | var i uint | |
465 | for i = 0; i < uint(len(visit())); i++ { | |
466 | if addr[Visit] & (1 << i) != 0 && visit()[i] == data.i2p[addr[Location]] { | |
467 | other[Visit] = addr[Visit] & ^(1 << i) | |
468 | Consider(data, dims, table, other, 0, &best_value, best_source) | |
469 | } | |
470 | } | |
471 | other[Visit] = addr[Visit] | |
472 | ||
473 | /* Buy Eden warp units */ | |
474 | eden_limit := data.Commodities["Eden Warp Units"].Limit | |
475 | if addr[Edens] > 0 && addr[Edens] <= eden_limit { | |
476 | relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Eden Warp Units"] | |
477 | if available { | |
478 | absolute_price := int(float64(data.Commodities["Eden Warp Units"].BasePrice) * float64(relative_price) / 100.0) | |
479 | for quantity := addr[Edens]; quantity > 0; quantity-- { | |
480 | other[Edens] = addr[Edens] - quantity | |
481 | if addr[Hold] != 0 { | |
482 | other[UnusedCargo] = addr[UnusedCargo] + quantity | |
483 | } | |
484 | if other[UnusedCargo] < dims[UnusedCargo] { | |
485 | Consider(data, dims, table, other, -absolute_price * quantity, &best_value, best_source) | |
486 | } | |
487 | } | |
488 | other[Edens] = addr[Edens] | |
489 | other[UnusedCargo] = addr[UnusedCargo] | |
490 | } | |
491 | } | |
492 | ||
493 | // Check that we didn't lose track of any temporary modifications to other. | |
494 | for i := 0; i < NumDimensions; i++ { | |
495 | if addr[i] != other[i] { | |
496 | panic(i) | |
497 | } | |
498 | } | |
499 | ||
500 | // Sanity check: This cell was in state BEING_EVALUATED | |
501 | // the whole time that it was being evaluated. | |
502 | if table[my_index].value != CELL_BEING_EVALUATED { | |
503 | panic(my_index) | |
504 | } | |
505 | ||
506 | // Record our findings | |
507 | table[my_index].value = best_value | |
508 | table[my_index].from = EncodeIndex(dims, best_source) | |
509 | ||
510 | // UI: Progress bar | |
511 | cell_filled_count ++ | |
512 | if cell_filled_count & 0xff == 0 { | |
513 | print(fmt.Sprintf("\r%3.1f%%", 100*float64(cell_filled_count)/float64(StateTableSize(dims)))) | |
514 | } | |
515 | ||
516 | return table[my_index].value | |
517 | } | |
518 | ||
519 | func FindBestState(data planet_data, dims []int, table []State) int32 { | |
520 | addr := make([]int, NumDimensions) | |
521 | addr[Edens] = *end_edens | |
522 | addr[Cloaks] = dims[Cloaks] - 1 | |
523 | addr[BuyFighters] = dims[BuyFighters] - 1 | |
524 | addr[BuyShields] = dims[BuyShields] - 1 | |
525 | addr[Visit] = dims[Visit] - 1 | |
526 | addr[Traded] = 1 | |
527 | addr[Hold] = 0 | |
528 | addr[UnusedCargo] = 0 | |
529 | max_index := int32(-1) | |
530 | max_value := int32(0) | |
531 | max_fuel := 1 | |
532 | if *fuel == 0 { | |
533 | max_fuel = 0 | |
534 | } | |
535 | for addr[Fuel] = 0; addr[Fuel] <= max_fuel; addr[Fuel]++ { | |
536 | for addr[Location] = 0; addr[Location] < dims[Location]; addr[Location]++ { | |
537 | if len(end()) == 0 || end()[data.i2p[addr[Location]]] { | |
538 | index := EncodeIndex(dims, addr) | |
539 | value := CellValue(data, dims, table, addr) | |
540 | if value > max_value { | |
541 | max_value = value | |
542 | max_index = index | |
543 | } | |
544 | } | |
545 | } | |
546 | } | |
547 | return max_index | |
548 | } | |
549 | ||
550 | func Commas(n int32) (s string) { | |
551 | r := n % 1000 | |
552 | n /= 1000 | |
553 | for n > 0 { | |
554 | s = fmt.Sprintf(",%03d", r) + s | |
555 | r = n % 1000 | |
556 | n /= 1000 | |
557 | } | |
558 | s = fmt.Sprint(r) + s | |
559 | return | |
560 | } | |
561 | ||
562 | func DescribePath(data planet_data, dims []int, table []State, start int32) (description []string) { | |
563 | for index := start; index > 0 && table[index].from > 0; index = table[index].from { | |
564 | var line string | |
565 | addr := DecodeIndex(dims, index) | |
566 | prev := DecodeIndex(dims, table[index].from) | |
567 | if addr[Fuel] != prev[Fuel] { | |
568 | from := data.i2p[prev[Location]] | |
569 | to := data.i2p[addr[Location]] | |
570 | line += fmt.Sprintf("Jump from %v to %v (%v hyper jump units)", from, to, prev[Fuel]-addr[Fuel]) | |
571 | } | |
572 | if addr[Edens] == prev[Edens] - 1 { | |
573 | from := data.i2p[prev[Location]] | |
574 | to := data.i2p[addr[Location]] | |
575 | line += fmt.Sprintf("Eden warp from %v to %v", from, to) | |
576 | } | |
577 | if addr[Hold] != prev[Hold] { | |
578 | if addr[Hold] == 0 { | |
579 | quantity := *hold - (prev[UnusedCargo] + prev[Edens] + prev[Cloaks]) | |
580 | line += fmt.Sprintf("Sell %v %v", quantity, data.i2c[prev[Hold]]) | |
581 | } else if prev[Hold] == 0 { | |
582 | quantity := *hold - (addr[UnusedCargo] + addr[Edens] + addr[Cloaks]) | |
583 | line += fmt.Sprintf("Buy %v %v", quantity, data.i2c[addr[Hold]]) | |
584 | } else { | |
585 | panic("Switched cargo?") | |
586 | } | |
587 | ||
588 | } | |
589 | if addr[Cloaks] == 1 && prev[Cloaks] == 0 { | |
590 | // TODO: Dump cloaks, convert from cargo? | |
591 | line += "Buy a Cloak" | |
592 | } | |
593 | if addr[Edens] > prev[Edens] { | |
594 | line += fmt.Sprint("Buy ", addr[Edens] - prev[Edens], " Eden Warp Units") | |
595 | } | |
596 | if addr[BuyShields] == 1 && prev[BuyShields] == 0 { | |
597 | line += fmt.Sprint("Buy ", *batteries, " Shield Batterys") | |
598 | } | |
599 | if addr[BuyFighters] == 1 && prev[BuyFighters] == 0 { | |
600 | line += fmt.Sprint("Buy ", *drones, " Fighter Drones") | |
601 | } | |
602 | if addr[Visit] != prev[Visit] { | |
603 | // TODO: verify that the bit chat changed is addr[Location] | |
604 | line += fmt.Sprint("Visit ", data.i2p[addr[Location]]) | |
605 | } | |
606 | if line == "" && addr[Hold] == prev[Hold] && addr[Traded] != prev[Traded] { | |
607 | // The Traded dimension is for housekeeping. It doesn't directly | |
608 | // correspond to in-game actions, so don't report transitions. | |
609 | continue | |
610 | } | |
611 | if line == "" { | |
612 | line = fmt.Sprint(prev, " -> ", addr) | |
613 | } | |
614 | description = append(description, fmt.Sprintf("%13v ", Commas(table[index].value)) + line) | |
615 | } | |
616 | return | |
617 | } | |
618 | ||
619 | // (Example of a use case for generics in Go) | |
620 | func IndexPlanets(m *map[string]Planet, start_at int) (map[string]int, []string) { | |
621 | e2i := make(map[string]int, len(*m)+start_at) | |
622 | i2e := make([]string, len(*m)+start_at) | |
623 | i := start_at | |
624 | for e := range *m { | |
625 | e2i[e] = i | |
626 | i2e[i] = e | |
627 | i++ | |
628 | } | |
629 | return e2i, i2e | |
630 | } | |
631 | func IndexCommodities(m *map[string]Commodity, start_at int) (map[string]int, []string) { | |
632 | e2i := make(map[string]int, len(*m)+start_at) | |
633 | i2e := make([]string, len(*m)+start_at) | |
634 | i := start_at | |
635 | for e := range *m { | |
636 | e2i[e] = i | |
637 | i2e[i] = e | |
638 | i++ | |
639 | } | |
640 | return e2i, i2e | |
641 | } | |
642 | ||
643 | func main() { | |
644 | flag.Parse() | |
645 | if *cpuprofile != "" { | |
646 | f, err := os.Create(*cpuprofile) | |
647 | if err != nil { | |
648 | panic(err) | |
649 | } | |
650 | pprof.StartCPUProfile(f) | |
651 | defer pprof.StopCPUProfile() | |
652 | } | |
653 | data := ReadData() | |
654 | if *drone_price > 0 { | |
655 | temp := data.Commodities["Fighter Drones"] | |
656 | temp.BasePrice = *drone_price | |
657 | data.Commodities["Fighter Drones"] = temp | |
658 | } | |
659 | if *battery_price > 0 { | |
660 | temp := data.Commodities["Shield Batterys"] | |
661 | temp.BasePrice = *battery_price | |
662 | data.Commodities["Shield Batterys"] = temp | |
663 | } | |
664 | data.p2i, data.i2p = IndexPlanets(&data.Planets, 0) | |
665 | data.c2i, data.i2c = IndexCommodities(&data.Commodities, 1) | |
666 | dims := DimensionSizes(data) | |
667 | table := CreateStateTable(data, dims) | |
668 | best := FindBestState(data, dims, table) | |
669 | print("\n") | |
670 | if best == -1 { | |
671 | print("Cannot acheive success criteria\n") | |
672 | } else { | |
673 | description := DescribePath(data, dims, table, best) | |
674 | for i := len(description) - 1; i >= 0; i-- { | |
675 | fmt.Println(description[i]) | |
676 | } | |
677 | } | |
678 | } |