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